Skip to content

Instantly share code, notes, and snippets.

@mnikn
Created February 27, 2019 01:48
Show Gist options
  • Save mnikn/8244a146b8aff78d27c779a60245d8ea to your computer and use it in GitHub Desktop.
Save mnikn/8244a146b8aff78d27c779a60245d8ea to your computer and use it in GitHub Desktop.
[debounce] debounce and throttle #utils
function debounce(fn, delay) {
let timer = setTimeout(fn, delay);
return function(){
clearTimeout(timer);
fn.apply(this, arguments);
};
}
function throttle(fn, delay) {
let timer = setTimeout(fn, delay);
let startTime = new Date();
return function() {
clearTimeout(timer);
let currentTime = new Date();
if(currentTime - startTime > delay) {
fn.apply(this, arguments);
startTime = currentTime;
} else {
timer = = setTimeout(fn, delay);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment