Last active
November 11, 2015 11:30
-
-
Save matthijn/f1a1433387f92719a510 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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