UNPKG

web-utils-super

Version:

前端函数库

28 lines (26 loc) 914 B
/** * @desc: 防抖函数,设置一个定时器,约定在xx毫秒后再触发事件处理,每次触发事件都会重新设置计时器,直到xx毫秒内无第二次操作 * @param {Function} func * @param {Number} wait * @param {Boolean} promptly 默认false: wait毫秒内无第二次操作触发 | true:立即触发一次,wait毫秒内无第二次操作清空计时器,然后再次操作即可触发func * @return {Function} */ function debounce(func, wait, promptly = false) { let timeout = null return function () { let context = this let args = arguments function clear() { timeout = undefined } function exec() { func.apply(context, args) } if (promptly && !timeout) { exec() } if (timeout) clearTimeout(timeout) timeout = setTimeout(promptly ? clear : exec, wait) } } module.exports = debounce