Last active
August 29, 2015 14:15
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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