Skip to content

Instantly share code, notes, and snippets.

@oliverfoster
Created December 13, 2017 12:42
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 oliverfoster/38c377f4db44759a6abf8de75bf6cdb7 to your computer and use it in GitHub Desktop.
Save oliverfoster/38c377f4db44759a6abf8de75bf6cdb7 to your computer and use it in GitHub Desktop.
Cancellable debounce
function debounce(cb, time) {
var handle = null;
var func = function() {
func.cancel();
handle = setTimeout(cb, time);
};
func.cancel = function() {
if (!handle) return;
clearTimeout(handle);
handle = null;
};
return func;
}
var func = debounce(function() { console.log("call"); }, 200);
func();
func();
func();
func.cancel();
func();
func();
func();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment