小型喉管
这是提供 throttle 财务报告和审定财务报表 debounce 函数,两者兼具
流程和类型脚本声明,以及最小的代码脚印(小于60个)
线条,小于 350 字节已修改+gzipped)
节奏 节奏 和一切之间
type ThrottleOptions = {
start?: boolean, // fire immediately on the first call
middle?: boolean, // if true, fire as soon as `wait` has passed
once?: boolean, // cancel after the first successful call
}
function throttle<T>(
callback: (...args: T[]) => any,
wait: number,
opts?: ThrottleOptions
): (...args: T[]) => void
function debounce<T>(
callback: (...args: T[]) => any,
wait: number,
opts?: ThrottleOptions
): (...args: T[]) => void
这个软件包有两个功能; throttle 财务报告和审定财务报表 debounce。 。 。 .
这两个功能都提供了完全相同的签名,因为两者都是
相同的功能 - 只是与不同的 opts 默认 :
throttle选择默认为{ start: true, middle: true, once: false }。 。 。 .debounce选择默认为{ start: false, middle: false, once: false }。 。 。 .
每个选项在 callback 被调用。 最好的办法
用大理石图来说明这一点.
for (let i = 1; i <= 10; ++i) {
fn(i)
await delay(50)
}
await delay(100)
| fn() | 1 2 3 4 5 6 7 8 9 10 |
| throttle(fn, 100) | 1 2 4 6 8 10 |
| throttle(fn, 100, {start: false}) | 2 4 6 8 10 |
| throttle(fn, 100, {middle: false}) | 1 10 |
| throttle(fn, 100, {once: true}) | 1 |
| throttle(fn, 100, {once: true, start: false})| 2 |
| debounce(fn, 100) | 10 |
TypeScript 装饰器支持 !
这个包还包括一个可以用来提供 类型脚本装饰器 功能说明.
这是一个例子,显示你需要做什么:
import {throttle} from '@github/mini-throttle/decorators'
// ^ note: add `/decorators` to the import to get decorators
class MyClass {
@throttle(100, { start: false }) // <- Just like normal throttle, but you omit the callback argument
doThings() {
// `MyClass.prototype.doThings` will be throttled!
}
}