petite-utils
Version:
A collection of small util function in JavaScript.
41 lines (40 loc) • 1.19 kB
TypeScript
/**
* @description: stop run repeat-function
*/
export type StopRepeat = () => void;
export type RepeatFn = (repeatTimes: number, stop: StopRepeat, params?: unknown) => void;
export interface RepeatOptions {
interval?: number;
immediate?: boolean;
}
/**
* ## repeat call function
* @group 工具函数
* @param fn will be called repeatly
* @param param1.interval interval time default 1000
* @param param1.immediate call fn immediately default false
* @param params params pass to fn
* @returns stopFunction to stop repeat
* @example
* ### pass a arrow function to repeat
* ```ts
* repeatRun((repeatTimes, stop) => {
* console.log(repeatTimes)
* if (repeatTimes === 5) {
* stop() // stop function from fn params
* }
* })
* ```
* @example
* ### pass a function definition to repeat and stop it by repeat return value
* ```ts
* let stop = repeatRun(sayHi, { interval: 1000 })
* function sayHi(repeatTimes) {
* console.log(repeatTimes)
* if (repeatTimes === 5) {
* stop() // stop from repeatRun return value
* }
* }
* ```
*/
export declare function repeatRun(fn: RepeatFn, { interval, immediate }?: RepeatOptions, params?: unknown): StopRepeat;