Skip to content

Instantly share code, notes, and snippets.

@victorpavlov
Created September 21, 2020 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorpavlov/dffffb216914cbd73ea639f79b4a4dfb to your computer and use it in GitHub Desktop.
Save victorpavlov/dffffb216914cbd73ea639f79b4a4dfb to your computer and use it in GitHub Desktop.
function delay(callback, ms) {
var timer = 0;
return function() {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
callback.apply(context, args);
}, ms || 0);
};
}
// Example usage:
$('input').keyup(delay(function (e) {
console.log('Time elapsed!', this.value);
}, 500));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment