Created
June 4, 2012 22:26
-
-
Save lukmdo/2871182 to your computer and use it in GitHub Desktop.
self-delaying function
This file contains hidden or 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
// quite same what _.debounce does http://underscorejs.org/#debounce | |
function self_delay(delay, just_run){ | |
delay = delay || 3000; | |
clearTimeout(self_delay.sheduled); | |
if (!just_run){ | |
self_delay.sheduled = setTimeout(self_delay, delay, 0, true); | |
return; | |
} | |
// logic/code comes here... | |
console.log('Hello - not more them once a %sms', delay); | |
}; | |
// data fetching is only example could be any async code for ie touching the DOM | |
var urls = []; | |
$.each(urls, function(i, url){ | |
$.get(url, function(data){ | |
console.log('got data back from %s', url); | |
self_delay(2000); | |
}); | |
}); | |
// this could be turned into self delayed function generator ?! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment