Skip to content

Instantly share code, notes, and snippets.

@chekit
Created August 5, 2017 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chekit/ece8db0b170a1be30d328c22a52d3648 to your computer and use it in GitHub Desktop.
Save chekit/ece8db0b170a1be30d328c22a52d3648 to your computer and use it in GitHub Desktop.
Debounce and Throttle methods
/*
Debounce
*/
export function debounce(fn, time) {
let timeout = null;
return function () {
let context = this;
let args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function () {
timeout = null;
fn.apply(context, args);
}, time);
}
}
/*
Throttle
*/
export function throttle(fn, time) {
let timeout = null;
return function () {
let context = this;
let args = arguments;
if (!timeout) {
fn.apply(context, args);
timeout = setTimeout(function () {
timeout = null;
}, time);
}
}
}
// http://jsbin.com/zinarem/edit?js,console,output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment