Skip to content

Instantly share code, notes, and snippets.

@qzm
Created June 8, 2017 05:48
Show Gist options
  • Save qzm/b68443df9833e9d6bb513ec8d2c1fb18 to your computer and use it in GitHub Desktop.
Save qzm/b68443df9833e9d6bb513ec8d2c1fb18 to your computer and use it in GitHub Desktop.
JavaScript debounce ES6
/**
* 防止函数过多的执行,默认十秒钟
* @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