Skip to content

Instantly share code, notes, and snippets.

@DanWebb
Last active August 29, 2015 14:15
Show Gist options
  • Save DanWebb/cd91cc67bf1b85068317 to your computer and use it in GitHub Desktop.
Save DanWebb/cd91cc67bf1b85068317 to your computer and use it in GitHub Desktop.
Set a waiting period on any number of different actions definable by name and time parameters, returns true if the action can be executed or false if it is being called again within the waiting period.
var wait = (function() {
var timers = {};
return function(name, time) {
// if the current timer has not been set yet or it has previously
// been set but finished it's waiting period
if(typeof(timers[name])==='undefined' || timers[name]) {
timers[name] = false;
setTimeout(function() { timers[name] = true; }, time);
return true;
}
// The timer hasn't finished it's waiting period
if(!timers[name])
return false;
};
})();
// example usage
$('select').change(function() {
if(!wait('change', 200))
return;
// do stuff
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment