# requestidlecallback

> A polyfill for the requestIdleCallback.

Latest version **0.3.0** (published 2016-10-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install requestidlecallback
pnpm add requestidlecallback
yarn add requestidlecallback
bun add requestidlecallback
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2016-10-23 |
| First published | 2015-11-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/requestidlecallback) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 184 |
| Author | Alexander Farkas |
| Maintainers | afarkas |

## Links

- npm: https://www.npmjs.com/package/requestidlecallback
- Repository: https://github.com/aFarkas/requestIdleCallback
- Homepage: https://github.com/aFarkas/requestIdleCallback#readme
- Issues: https://github.com/aFarkas/requestIdleCallback/issues
- npm.io page: https://npm.io/package/requestidlecallback

## Recent versions

- 0.3.0 (latest) — 2016-10-23
- 0.2.0 — 2015-11-01
- 0.1.0 — 2015-11-01

## README

#`requestIdleCallback` polyfill/shim [![Build Status](https://api.travis-ci.org/aFarkas/requestIdleCallback.svg?branch=master)](https://travis-ci.org/aFarkas/requestIdleCallback)

This is a polyfill/shim for the `requestIdleCallback` and `cancelIdleCallback` API. Also fixes early API implementation. 

For more information see the [Cooperative Scheduling of Background Tasks Draft](http://www.w3.org/TR/requestidlecallback/).

##Installation
Include the "index.js" in your website and use `requestIdleCallback` and `cancelIdleCallback` according to the specification.

##How it works
`requestIdleCallback` can't be really polyfilled. Therefore `requestIdleCallback` basically includes a throttle like function, that uses some heuristics to detect a) long running frames and b) user input as also DOM mutations to adapt accordingly. `requestIdleCallback` also tries to get the time right after a frame commit. The `deadline.timeRemaining()` either starts with 7ms or with 22ms for the first scheduled callback.

If multiple functions are scheduled with the `requestIdleCallback` shim for the same idle time, the shim makes sure to split those functions as soon as `timeRemaining()` is exceeded.

##Usage

If you have a fast or a non-splittable task:

```js
requstIdleCallback(function(){
	//your task
});
```

In case you have a heavy and splittable task you can use efficient script yielding technique:

```js
requestIdleCallback(function(deadline){
	while(tasks.length && deadline.timeRemaining() > 0){
		tasks.shift()();
	}
	
	if(tasks.length){
		requestIdleCallback(runTasks);
	}
});
```

**Reading vs writing layout:** `requestIdleCallback` is mainly for layout neutral or layout reading/measuring tasks. In case you want to write layout/manipulate the DOM consider using `requestAnimationFrame` instead.

Of course `requestIdleCallback` can also be combined with `requestAnimationFrame`:

```js
requstIdleCallback(function(){
	var width = element.offsetWidth;
	
	requestAnimationFrame(function(){
		element.classList[width > 600 ? 'add' : 'remove']('is-large');
	});
});
```

---
_Source: https://npm.io/package/requestidlecallback · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
