Skip to content

Instantly share code, notes, and snippets.

@dotfold
Last active December 17, 2015 07:39
Show Gist options
  • Save dotfold/5573946 to your computer and use it in GitHub Desktop.
Save dotfold/5573946 to your computer and use it in GitHub Desktop.
(function() {
var debounce = function (callback, wait) {
var timeout,
thisObj;
return function() {
var later = function() {
timeout = null;
callback.apply(thisObj);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
var fn = debounce(function() {
console.log('debounced');
}, 500);
//fn();
// call with interval > bouncerate
//setInterval(fn, 600); // should always call
// call with interval < wait period
var count = 0;
var intId = setInterval(function() {
fn();
if (++count == 10) {
console.log(count);
clearInterval(intId);
}
}, 300); // should not call for 10 goes
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment