Skip to content

Instantly share code, notes, and snippets.

@iadvize
Created March 18, 2011 17:11
Show Gist options
  • Save iadvize/876448 to your computer and use it in GitHub Desktop.
Save iadvize/876448 to your computer and use it in GitHub Desktop.
a fonction to run some code no more than 1 time per period
function runOnce(wait, cb){
var timeOut = null
, launched = false;
return function(){
if (launched === false){
launched = true;
cb(arguments);
}
if(timeOut){
return;
}
timeOut = setTimeout(function(){
launched = false;
timeOut = null;
}, wait);
};
}
/* How to use:
var tesFunc = runOnce(3000, function (args) {
console.debug('testFunc executed');
});
tesFunc(1);tesFunc(1);tesFunc(1);
setTimeout(function(){tesFunc(1);tesFunc(1);},5000);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment