@tempots/std

delayed() function

Delays the execution of a function by the given number of milliseconds.

Signature:

delayed: (fn: () => void, ms: number) => () => void

Parameters

Parameter

Type

Description

fn

() => void

The function to delay.

ms

number

The number of milliseconds to delay the function.

Returns: () => void

A function that, when called, will cancel the delay and prevent the original function from being executed.

Example

// Delay a function for 1 second
const cancel = delayed(() => console.log('Hello!'), 1000);

// Cancel the delayed execution if needed
cancel();