no message
This commit is contained in:
+21
-1
@@ -1,4 +1,7 @@
|
||||
|
||||
let timeout = null;
|
||||
let timer;
|
||||
let flag;
|
||||
|
||||
|
||||
export default {
|
||||
@@ -37,7 +40,24 @@ export default {
|
||||
}
|
||||
return tmpTime;
|
||||
},
|
||||
|
||||
debounce(func,wait = 1000,immediate = true){
|
||||
// 清除定时器
|
||||
if (timeout !== null) clearTimeout(timeout)
|
||||
// 立即执行,此类情况一般用不到
|
||||
if (immediate) {
|
||||
const callNow = !timeout
|
||||
timeout = setTimeout(() => {
|
||||
timeout = null
|
||||
}, wait)
|
||||
|
||||
if (callNow) typeof func === 'function' && func()
|
||||
} else {
|
||||
// 设置定时器,当最后一次操作后,timeout不会再被清除,所以在延时wait毫秒后执行func回调方法
|
||||
timeout = setTimeout(() => {
|
||||
typeof func === 'function' && func()
|
||||
}, wait)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user