Skip to content

Instantly share code, notes, and snippets.

@severuykhin
Created April 2, 2017 12:29
Show Gist options
  • Save severuykhin/3c22576f08d65fafbf9f64a1927cb4ef to your computer and use it in GitHub Desktop.
Save severuykhin/3c22576f08d65fafbf9f64a1927cb4ef to your computer and use it in GitHub Desktop.
Converts several function calls within a certain time into one call
var debounce = function (f, ms){
var state = null;
var COOLDOWN = 1;
return function(){
if(state) return;
f.apply(this. arguments);
state = COOLDOWN;
setTimeout(function(){state = null}, ms);
}
}
let width = 1;
var action = function(){
document.querySelector('#text').style.transform = `scale(1.${width})`;
width++;
}
document.querySelector('button').onclick = debounce(action, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment