Request Idle Callback Polyfill
npm install @horat1us/request-idle-callbackAvailable as commonjs and esm module with typescript typings.
See Using requestIdleCallback
for details.
This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the spec.
Includes TypeScript typings and source code.
bash
npm i @horat1us/request-idle-callback
`Usage
Most common usage:
`javascriptvar handler = require('@horat1us/request-idle-callback')();
/ or /
handler = require('@horat1us/request-idle-callback/polyfill')();
// returns native requestIdleCallback/cancelIdleCallback if compliant
handler.requestIdleCallback;
handler.cancelIdleCallback;
`
ESM module also available:
`javascript
import { requestIdleCallback, cancelIdleCallback } from "@horat1us/request-idle-callback"
`To shim (replace global) you need to use:
`javascript
require("@horat1us/request-idle-callback").shim();
// or
require("@horat1us/request-idle-callback/auto");
`
Or ESM (prefered):
`javascript
import {shim} from "@horat1us/request-idle-callback/esm";
shim();
`$3
You can also use IdlePromise.
Note: Promise support in runtime environment is required
`javascript
import { IdlePromise, IdlePromiseCancel } from "./src/IdlePromise";// Create Promise
const promise = IdlePromise({
timeout: 5000,
});
// Process event, handle rejections
promise
.then((deadline) => {
// resolved, same as requestIdleCallback
console.log(deadline)
})
.catch((error) => {
if (error.message === "Idle Promise Cancel") {
// handle cancellation
return;
}
// handle rejection
});
// You can also cancel idleCallack resolving
promise.cancel("Some Reason");
``