Created
August 1, 2017 10:14
-
-
Save vace/b1126fcff2beb660ea5688d1700059ae to your computer and use it in GitHub Desktop.
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(func, wait, immediate){ | |
var timeout, args, context, timestamp, result; | |
if (null == wait) wait = 100; | |
function later() { | |
var last = Date.now() - timestamp; | |
if (last < wait && last >= 0) { | |
timeout = setTimeout(later, wait - last); | |
} else { | |
timeout = null; | |
if (!immediate) { | |
result = func.apply(context, args); | |
context = args = null; | |
} | |
} | |
}; | |
var debounced = function(){ | |
context = this; | |
args = arguments; | |
timestamp = Date.now(); | |
var callNow = immediate && !timeout; | |
if (!timeout) timeout = setTimeout(later, wait); | |
if (callNow) { | |
result = func.apply(context, args); | |
context = args = null; | |
} | |
return result; | |
}; | |
debounced.clear = function() { | |
if (timeout) { | |
clearTimeout(timeout); | |
timeout = null; | |
} | |
}; | |
debounced.flush = function() { | |
if (timeout) { | |
result = func.apply(context, args); | |
context = args = null; | |
clearTimeout(timeout); | |
timeout = null; | |
} | |
}; | |
return debounced; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment