提交 34c7c04d 编写于 作者: Q qiang

fix: Can only call Window.clearTimeout on instances of Window

上级 be4f8e78
......@@ -9,15 +9,19 @@ interface Timer {
* @param timer
* @returns
*/
export function debounce(fn: Function, delay: number, timer: Timer) {
export function debounce(
fn: Function,
delay: number,
{ clearTimeout, setTimeout }: Timer
) {
let timeout: any
const newFn = function (this: any) {
timer.clearTimeout(timeout)
clearTimeout(timeout)
const timerFn = () => fn.apply(this, arguments)
timeout = timer.setTimeout(timerFn, delay)
timeout = setTimeout(timerFn, delay)
}
newFn.cancel = function () {
timer.clearTimeout(timeout)
clearTimeout(timeout)
}
return newFn
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册