Skip to content

Instantly share code, notes, and snippets.

@matthijn
Last active November 11, 2015 11:30
Show Gist options
  • Save matthijn/f1a1433387f92719a510 to your computer and use it in GitHub Desktop.
Save matthijn/f1a1433387f92719a510 to your computer and use it in GitHub Desktop.
/**
* Helper for delaying certain callbacks
* @param callback
* @param delay
* @constructor
*/
define('Delay', [], function()
{
return function(callback, delay)
{
this.timeOutId = 0;
this.start = function()
{
// And create a new timeout
this.timeOutId = setTimeout(callback, delay);
};
this.reset = function()
{
// Clear the old timeout
clearTimeout(this.timeOutId);
// And restart
this.start();
};
};
});
// Usage
var delay = new Delay(function(){
// Some code here after delay
}, 300); // 300ms
delay.reset(); // Na elke onkeyup wordt reset aangeroepen wat de timeout verwijderd en een nieuwe aanmaakt. (e.g als er binnen 300ms getyped wordt wordt die verwijderd en wordt er opnieuw 300ms gewacht)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment