Salesforce OA 面试题解析:JavaScript 中实现 Debounce(防抖)

34次阅读
没有评论

Implement debounce in JavaScript.

Write a function debounce that takes a function and a delay, and returns a new function that postpones invoking the original function until after the delay has elapsed since the last time the debounced function was called.

这道题考察的是前端常见的防抖(debounce)实现:当函数被连续频繁触发时,只有最后一次触发在指定延迟之后才真正执行。通常需要用闭包保存定时器句柄,并在每次调用时先清除旧的 timer,再重新设置新的 timer,从而保证输入框搜索、窗口 resize、按钮重复点击等场景只触发一次最终回调。

正文完
 0