Skip to main content

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โ€‹

setTimeout() - MDN


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();