Skip to content

Instantly share code, notes, and snippets.

@u01jmg3
Last active August 29, 2015 14:15
Show Gist options
  • Save u01jmg3/603d0e3fbc86678878cd to your computer and use it in GitHub Desktop.
Save u01jmg3/603d0e3fbc86678878cd to your computer and use it in GitHub Desktop.
Delays when a function runs. Perfect for using with a search box and waiting for a user to finish typing a term before executing a filter script.
/**
* Delays when a function runs.
*
* @return callback
*/
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
//delay(function(){console.log('Hello World!')}, 100); //milliseconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment