Skip to content

Instantly share code, notes, and snippets.

@pokka
Last active September 6, 2016 06:15
Show Gist options
  • Save pokka/5335363 to your computer and use it in GitHub Desktop.
Save pokka/5335363 to your computer and use it in GitHub Desktop.
simple delay javascript function,when you care about duplicate execute
let timer;
const delay = (func, ms = 500) => {
clearTimeout(timer);
timer = setTimeout(func, ms);
}
export default delay;
var delay = (function(){
var timer;
return function(func, ms){
clearTimeout(timer);
timer = setTimeout(func, ms);
};
})();
// USAGE
// delay(function(){console.log("you won't see me")},1000);
// delay(function(){console.log("you won't see me")},1000);
// delay(function(){console.log("here!")},1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment