@horat1us/request-idle-callback
v1.0.6
Published
Request Idle Callback Polyfill
Downloads
49
Maintainers
Readme
IdleCallback
Available 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.
Installation
Using npm:
npm i @horat1us/request-idle-callback
Usage
Most common usage:
var 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:
import { requestIdleCallback, cancelIdleCallback } from "@horat1us/request-idle-callback"
To shim (replace global) you need to use:
require("@horat1us/request-idle-callback").shim();
// or
require("@horat1us/request-idle-callback/auto");
Or ESM (prefered):
import {shim} from "@horat1us/request-idle-callback/esm";
shim();
IdlePromise
You can also use IdlePromise.
Note: Promise support in runtime environment is required
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");