Skip to content

Instantly share code, notes, and snippets.

@dondevi
Created October 2, 2018 12:48
Show Gist options
  • Save dondevi/466a3fabeb878de7ee7344c4fbaf900d to your computer and use it in GitHub Desktop.
Save dondevi/466a3fabeb878de7ee7344c4fbaf900d to your computer and use it in GitHub Desktop.
function debounce (fn, delay) {
var timer = -1;
return function () {
var that = this;
var args = arguments;
window.clearTimeout(timer);
timer = window.setTimeout(function () {
fn.apply(that, args);
}, delay || 300);
};
}
function throttle (fn, delay) {
var on = true;
return function () {
if (!on) { return; }
on = false;
var that = this;
var args = arguments;
window.setTimeout(function () {
fn.apply(that, args);
on = true;
}, delay || 300);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment