Created
December 13, 2017 12:42
-
-
Save oliverfoster/38c377f4db44759a6abf8de75bf6cdb7 to your computer and use it in GitHub Desktop.
Cancellable debounce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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