delay()
delay<
Args>(func,wait, ...args):Timeout
Invokes func after wait milliseconds.
DEPRECATED
Use setTimeout(fn, wait) directly instead.
Type Parametersโ
Args: Args extends unknown[]โ
Parametersโ
func: (...args) => voidโ
The function to delay.
wait: numberโ
The number of milliseconds to delay invocation.
args: ...Argsโ
The arguments to invoke the function with.
Returns: Timeoutโ
The timer id.
See Alsoโ
Sinceโ
1.0.0
Also known asโ
delay (Lodash, es-toolkit) ยท โ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)
Exampleโ
// โ Deprecated approach
delay(console.log, 1000, 'later');
// โ
Recommended approach
setTimeout(() => console.log('later'), 1000);
How it works?โ
Invokes func after wait milliseconds.
Deprecated: Use setTimeout() directly.
Native Equivalentโ
// โ delay(fn, 1000, arg1)
// โ
setTimeout(() => fn(arg1), 1000)
Use Casesโ
Delay execution ๐โ
Execute function after specified time.
setTimeout(() => {
showNotification();
}, 2000);
Debounce manuallyโ
Simple debounce pattern.
let timer;
const delayed = () => {
clearTimeout(timer);
timer = setTimeout(action, 300);
};
Sequence operationsโ
Chain timed operations.
await new Promise(r => setTimeout(r, 1000));
nextStep();