delay
function
Invokes func after wait milliseconds.
Installation
Import
import { delay } from '@tulx/utils';Source Code
Implementation
/**
* Invokes func after wait milliseconds.
*
* @param func - The function to delay.
* @param wait - The number of milliseconds to delay invocation.
* @param args - The arguments to invoke func with.
* @returns Returns the timer id.
*
* @example
* ```ts
* delay((text: string) => console.log(text), 1000, 'later'); // Logs 'later' after one second.
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function delay<T extends (...args: any[]) => any>(
func: T,
wait: number,
...args: Parameters<T>
): ReturnType<typeof setTimeout> {
return setTimeout(() => func(...args), wait);
}
Example
import { delay } from '@tulx/utils';
delay((text: string) => console.log(text), 1000, 'later'); // Logs 'later' after one second.Related Functions
after
The opposite of before; this method creates a function that invokes func once it's called n or more times.
ary
Creates a function that invokes func, with up to n arguments, ignoring any additional arguments.
before
Creates a function that invokes func, with the this binding and arguments of the created function, while it's called less than n times.
bind
Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives.
bindKey
Creates a function that invokes the method at object[key] with partials prepended to the arguments it receives.
curry
Creates a function that accepts arguments of func and either invokes func returning its result, or returns a function that accepts the remaining arguments.