25 lines
805 B
Markdown
25 lines
805 B
Markdown
# Background task
|
|
|
|
An simple function wrapper around
|
|
[requestIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback)
|
|
that queues tasks and runs them sequentially.
|
|
|
|
The main purpose is to be able to run maintainance functions that are not a good fit for a web
|
|
worker context (i.e. methods that require the DOM).
|
|
|
|
# Usage
|
|
|
|
Wrap your function.
|
|
|
|
```
|
|
const task = backgroundTask(() => {
|
|
// do stuff
|
|
});
|
|
```
|
|
|
|
Call `task` as you would any other function that has no return value. The return value of a
|
|
background task is true if the task was queued to `requestIdleCallback` immediately or `false` if there are previous invocations queued ahead of it.
|
|
|
|
Calling a task multiple times will queue the passed parameters and execute the task sequentially the same number of times.
|
|
|