Aller au contenu principal

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