Skip to content

fseehawer/requestidlecallback

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

requestIdleCallback polyfill

This requestIdleCallback polyfill doesn't use the window object to detect if the feature is supported in the browser and falls back to a simple timeout if the feature isn't supported.

install

npm install @fseehawer/requestidlecallback

example

import requestIdleCallback from '@fseehawer/requestidlecallback'

requestIdleCallback(() => {
  // your non-splittable task
})

for splittable task you can use the yielding technique:

requestIdleCallback((deadline) => {
  while ((deadline.timeRemaining() > 0) || deadline.didTimeout) {
    // your splittable tasks
  }
})

Using the Promise-based API

import requestIdleCallback from '@fseehawer/requestidlecallback';

requestIdleCallback.promise({ timeout: 100 }).then((deadline) => {
// Use the idle deadline to perform tasks
});

for cancelling?

import requestIdleCallback, { cancelIdleCallback } from '@fseehawer/requestidlecallback'

// your code
const id = requestIdleCallback(doSomething)

// then if you need to cancel
cancelIdleCallback(id)

license

MIT License

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors