File "Throttle.ts"

Full path: /home/fsibplc/public_html/sommilito-bank2/splide-4.1.3/src/js/constructors/Throttle/Throttle.ts
File size: 758 B (758 B bytes)
MIME-type: text/x-java
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

import { AnyFunction } from '../../types';
import { RequestInterval } from '../RequestInterval/RequestInterval';


/**
 * The interface for the returning value of the RequestInterval.
 *
 * @since 3.0.0
 */
export interface ThrottleInstance<F extends AnyFunction> extends Function {
  ( ...args: Parameters<F> ): void;
}

/**
 * Returns the throttled function.
 *
 * @param func     - A function to throttle.
 * @param duration - Optional. Throttle duration in milliseconds.
 *
 * @return A throttled function.
 */
export function Throttle<F extends AnyFunction>(
  func: F,
  duration?: number
): ThrottleInstance<F> {
  const interval = RequestInterval( duration || 0, func, null, 1 );

  return () => {
    interval.isPaused() && interval.start();
  };
}