Created
June 8, 2017 05:48
-
-
Save qzm/b68443df9833e9d6bb513ec8d2c1fb18 to your computer and use it in GitHub Desktop.
JavaScript debounce ES6
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
/** | |
* 防止函数过多的执行,默认十秒钟 | |
* @param {any} func 待修改的函数 | |
* @param {number} [wait=10000] 等待时间 | |
*/ | |
function debounce(func, wait = 10000) { | |
let lastTime = ''; | |
return (...argv) => { | |
let thisTime = new Date(); | |
if (!lastTime || thisTime - lastTime > wait) { | |
lastTime = thisTime; | |
func.apply(null, argv); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment