UNPKG

ut2

Version:

一个现代 JavaScript 实用工具库。[点击查看在线文档]。

43 lines (42 loc) 1.41 kB
import { FunctionAny } from './internals/types'; /** * 创建一个节流函数,该函数在 `wait` 毫秒数内最多执行一次 `func` 方法。 * * 节流函数还提供以下方法: * 1. `cancel` 方法取消延迟的函数调用。 * 2. `flush` 方法立即调用。 * 3. `pending` 方法是否在等待函数执行。 * * @alias module:Function.throttle * @since 1.0.0 * @param {Function} func 要节流的函数。 * @param {number} [wait=0] 需要节流的毫秒数。默认 `0` * @param {boolean} [immediate=true] 是否在节流开始前调用。默认 `true` * @returns 节流函数。 * @example * * function updatePosition(e){ * console.log('update position event: ', e); * } * * // 避免在滚动时频繁的更新定位。 * window.addEventListener('scroll', throttle(updatePosition, 100)); * * const throttled = throttle(()=>{ * // do something * } * * // 点击按钮, 300 毫秒内最多执行一次。 * document.querySelector('button').addEventListener('click', throttled, 300)); * * // 取消节流调用。 * window.addEvenetListener('popstate', throttled.cancel); * */ declare function throttle<T extends FunctionAny>(func: T, wait?: number, immediate?: boolean): { (this: any, ...args: Parameters<T>): ReturnType<T>; cancel: () => void; flush: () => ReturnType<T>; pending: () => boolean; }; export default throttle;