Last active
January 14, 2019 09:32
-
-
Save userkang/783f1d27e53b0963a081a73528047f23 to your computer and use it in GitHub Desktop.
防抖函数
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 | |
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