Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ importance: 5

# 去抖装饰器

`debounce(f, ms)`装饰器的结果应该是一个包装器,它每隔几毫秒调用一次 `f`。
`debounce(f, ms)` 装饰器的结果应该是一个包装器,它最多允许每隔 “ms” 毫秒调用一次 `f`。

换句话说,当我们调用 “debounced” 函数时,它保证将忽略最接近的 “ms” 内发生的情况
换句话说,当我们多次调用 “debounced” 函数时,它保证忽略距离上次调用在 “ms” 毫秒内的调用

例如:

Expand All @@ -16,9 +16,9 @@ let f = debounce(alert, 1000);
f(1); // 立即执行
f(2); // 忽略

setTimeout( () => f(3), 100); // 忽略 (只过去了12 ms)
setTimeout( () => f(3), 100); // 忽略(只过去了 100 ms
setTimeout( () => f(4), 1100); // 运行
setTimeout( () => f(5), 1500); // 忽略 (离最后一次执行不超过1000 ms)
setTimeout( () => f(5), 1500); // 忽略(离上一次执行不超过 1000 ms
```

在实践中,当我们知道在如此短的时间内没有什么新的事情可以做时,`debounce` 对于那些用于检索/更新的函数很有用,所以最好不要浪费资源。
在实践中,对于那些用于检索/更新的函数而言,当我们知道在短时间内基本不会有什么新内容的时候,`debounce` 就显得很有用,所以最好不要浪费资源。
Comment thread
StringTek2019 marked this conversation as resolved.