Skip to content

Instantly share code, notes, and snippets.

@simonrenoult
Last active January 25, 2021 12:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save simonrenoult/7639261 to your computer and use it in GitHub Desktop.
Save simonrenoult/7639261 to your computer and use it in GitHub Desktop.
Managing timeout. Especially handy with autocomplete.
// Props to : http://nathanleclaire.com/blog/2013/11/16/the-javascript-question-i-bombed-in-an-interview-with-a-y-combinator-startup/
var processOnTime = function ( next, delay ) {
var d = delay;
if ( ! next ) {
throw new ReferenceError();
}
if ( ! d || isNaN ( d ) ) {
d = 200;
}
if ( this.timeoutID ) window.clearTimeout ( this.timeoutID );
this.timeoutID = setTimeout ( function () {
next();
}, d );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment