Skip to main content

对一个事件循环内所有调用进行防抖

image.png

但是这道题总结下来,应该是要实现两个需求: - 多次同步调用 G ,只执行第一次 - 分别使用 setTimeout 异步调用 G 和同步调用 G ,各执行一次

function debounce(func) {
let called = false
return function () {
if (!called) {
called = true
func()
Promise.resolve().then(() => {
called = false
})
}
}
}

参考文章: https://segmentfault.com/a/1190000039677990