# delayed
**Repository Path**: mirrors_rvagg/delayed
## Basic Information
- **Project Name**: delayed
- **Description**: A collection of JavaScript helper functions for your functions, using setTimeout() to delay and defer.
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-08-18
- **Last Updated**: 2026-03-21
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Delayed
**A collection of JavaScript helper functions for your functions, using `setTimeout()` to delay, defer and debounce**
[](https://nodei.co/npm/delayed/)
**Delayed** is designed for use across JavaScript runtimes, including the browser and within Node.js. It is available in npm as *"delayed"* or can be [downloaded](https://raw.github.com/rvagg/delayed/master/delayed.js).
## API
* [`delay()`](#delay)
* [`defer()`](#defer)
* [`delayed()`](#delayed)
* [`deferred()`](#deferred)
* [`debounce()`](#debounce) *(a.k.a `cumulativeDelayed()`)*
* [`noConflict()`](#noConflict)
---------------------------------------------
### `delay(fn, ms)`
`delay(fn, ms, context)`
`delay(fn, ms, context, arg1, arg2...)`
`delay()` is an interface to `setTimeout()` but with better `this` handling and consistent cross-browser argument passing.
Example:
```js
// print "beep" to the console after 1/2 a second
delayed.delay(() => console.log('beep'), 500)
function print (a, b) { console.log(this[a], this[b]) }
delayed.delay(print, 5000, { 'foo': 'Hello', 'bar': 'world' }, 'foo', 'bar')
// after 5 seconds, `print` is executed with the 3rd argument as `this`
// and the 4th and 5th as the arguments
```
`delay()` returns the timer reference from `setTimeout()` so it's possible to retain it and call `clearTimeout(timer)` to cancel execution.
---------------------------------------------
### `defer(fn)`
`defer(fn, context)`
`defer(fn, context, arg1, arg2...)`
`defer()` is essentially a shortcut for `delay(fn, 1...)`, which achieves a similar effect to `process.nextTick()` in Node.js or the proposed `setImmediate()` that we should start seeing in browsers soon (it exists in IE10). Use it to put off execution until the next time the browser/environment is ready to execute JavaScript. Given differences in timer resolutions across browsers, the exact timing will vary.
*Note: future versions of **delayed** will likely detect for and use `setImmediate()` for deferred functions.*
`defer()` returns the timer reference from `setTimeout()` so it's possible to retain it and call `clearTimeout(timer)` to cancel execution, as long as it's done within the same execution *tick*.
---------------------------------------------
### `delayed(fn, ms)`
`delayed(fn, ms, context)`
`delayed(fn, ms, context, arg1, arg2...)`
Returns a new function that will delay execution of the original function for the specified number of milliseconds when called.
Example:
```js
// a new function that will print "beep" to the console after 1/2 a second when called
var delayedBeeper = delayed.delay(() => console.log('beep'), 500)
delayedBeeper()
delayedBeeper()
delayedBeeper()
// 1/2 a second later we should see:
// beep
// beep
// beep
// each will have executed on a different timer
```
The new delayed function will return the timer reference from `setTimeout()` so it's possible to retain it and call `clearTimeout(timer)` to cancel execution *of that particular call*.
---------------------------------------------
### `deferred(fn)`
`deferred(fn, context)`
`deferred(fn, context, arg1, arg2...)`
Returns a new function that will defer execution of the original function, in the same manner that `defer()` defers execution.
The new delaying function will return the timer reference from `setTimeout()` so it's possible to retain it and call `clearTimeout(timer)` to cancel execution *of that particular call*, as long as it's done within the same execution *tick*.
---------------------------------------------
### `debounce(fn, ms)`
`debounce(fn, ms, context)`
`debounce(fn, ms, context, arg1, arg2...)`
_Also available with the name `cumulativeDelayed()`_
Returns a new function that will delay execution of the original function for the specified number of milliseconds when called. Execution will be **further delayed** for the same number of milliseconds upon each subsequent call before execution occurs.
The best way to explain this is to show its most obvious use-case: keyboard events in the browser.
```html