You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
881 B
30 lines
881 B
9 months ago
|
let timeout = null;
|
||
|
|
||
|
/**
|
||
|
* 防抖原理:一定时间内,只有最后一次操作,再过wait毫秒后才执行函数
|
||
|
*
|
||
|
* @param {Function} func 要执行的回调函数
|
||
|
* @param {Number} wait 延时的时间
|
||
|
* @param {Boolean} immediate 是否立即执行
|
||
|
* @return null
|
||
|
*/
|
||
|
function debounce(func, wait = 500, immediate = false) {
|
||
|
// 清除定时器
|
||
|
if (timeout !== null) clearTimeout(timeout);
|
||
|
// 立即执行,此类情况一般用不到
|
||
|
if (immediate) {
|
||
|
var callNow = !timeout;
|
||
|
timeout = setTimeout(function() {
|
||
|
timeout = null;
|
||
|
}, wait);
|
||
|
if (callNow) typeof func === 'function' && func();
|
||
|
} else {
|
||
|
// 设置定时器,当最后一次操作后,timeout不会再被清除,所以在延时wait毫秒后执行func回调方法
|
||
|
timeout = setTimeout(function() {
|
||
|
typeof func === 'function' && func();
|
||
|
}, wait);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default debounce
|