Skip to content

Instantly share code, notes, and snippets.

@userkang
Last active January 14, 2019 09:32
Show Gist options
  • Save userkang/783f1d27e53b0963a081a73528047f23 to your computer and use it in GitHub Desktop.
Save userkang/783f1d27e53b0963a081a73528047f23 to your computer and use it in GitHub Desktop.
防抖函数
function debounce(func, wait, immediate) {
var timeout
return function() {
var context = this
var args = arguments
if(timeout) clearTimeout(timeout)
if (immediate) {
var callNow = !timeout
timeout = setTimeout(function() {
timeout = null
}, wait)
if (callNow) func.apply(context, args)
} else {
timeout = setTimeout(function() {
func.apply(context, args)
}, wait)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment