框架
版本
Debouncer API 参考
Throttler API 参考
速率限制器 API 参考
队列 API 参考
批处理器 API 参考

createThrottler

函数:createThrottler()

ts
function createThrottler<TFn>(fn, initialOptions): SolidThrottler<TFn>
function createThrottler<TFn>(fn, initialOptions): SolidThrottler<TFn>

定义于:throttler/createThrottler.ts:59

一个低级别的 Solid 钩子,用于创建一个 Throttler 实例,该实例限制所提供函数的执行频率。

此钩子设计灵活且与状态管理无关——它仅返回一个节流器实例,您可以将其与任何状态管理解决方案(createSignal、Redux、Zustand、Jotai 等)集成。有关直接与 Solid 的 createSignal 集成的更简单、更高级别的钩子,请参阅 createThrottledSignal。

节流确保函数在指定的时间窗口内最多执行一次, 无论它被调用多少次。这对于限制昂贵的操作或 UI 更新的速率非常有用。

类型参数

TFn extends AnyFunction

参数

fn

TFn

initialOptions

ThrottlerOptions<TFn>

返回

SolidThrottler<TFn>

示例

tsx
// 带自定义状态的基础节流
const [value, setValue] = createSignal(0);
const throttler = createThrottler(setValue, { wait: 1000 });

// 结合任何状态管理器
const throttler = createThrottler(
  (value) => stateManager.setState(value),
  {
    wait: 2000,
    leading: true,   // 首次调用立即执行
    trailing: false  // 跳过尾随边缘更新
  }
);

// 通过信号访问节流器状态
console.log(throttler.executionCount()); // 已执行次数
console.log(throttler.isPending());      // 节流函数是否待处理
console.log(throttler.lastExecutionTime()); // 上次执行的时间戳
console.log(throttler.nextExecutionTime()); // 下次允许执行的时间戳
// 带自定义状态的基础节流
const [value, setValue] = createSignal(0);
const throttler = createThrottler(setValue, { wait: 1000 });

// 结合任何状态管理器
const throttler = createThrottler(
  (value) => stateManager.setState(value),
  {
    wait: 2000,
    leading: true,   // 首次调用立即执行
    trailing: false  // 跳过尾随边缘更新
  }
);

// 通过信号访问节流器状态
console.log(throttler.executionCount()); // 已执行次数
console.log(throttler.isPending());      // 节流函数是否待处理
console.log(throttler.lastExecutionTime()); // 上次执行的时间戳
console.log(throttler.nextExecutionTime()); // 下次允许执行的时间戳