• Stars
    star
    179
  • Rank 206,261 (Top 5 %)
  • Language
    JavaScript
  • Created over 8 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A requestIdleCallback shim/polyfill

requestIdleCallback polyfill/shim Build Status

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.

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:

requestIdleCallback(function(){
	//your task
});

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

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:

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

More Repositories

1

lazysizes

High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.
JavaScript
17,279
star
2

html5shiv

This script is the defacto way to enable use of HTML5 sectioning elements in legacy Internet Explorer.
JavaScript
9,876
star
3

webshim

[DEPRECATED] Webshims Lib is a modular capability-based polyfill-loading library
JavaScript
1,418
star
4

respimage

respimage is a responsive images polyfill, that loads your images fast and responsibly
JavaScript
779
star
5

jMediaelement

jquery multimedia framework
JavaScript
151
star
6

Ajaxmanager

An jQuery Plugin, wich helps you to manage AJAX requests and responses (i.e. abort requests, block requests, order requests)
JavaScript
98
star
7

wp-lazysizes

Brings lazySizes.js to WordPress
JavaScript
63
star
8

remote-list

super lightweight autocomplete
JavaScript
52
star
9

accessible--stylable-Radiobuttons-and-Checkboxes

helps you to create accessible and stylable custom checkboxes and radiobuttons
JavaScript
37
star
10

x-browser-accessors

cross browser DOM setter / getters (even for IE6)
JavaScript
9
star
11

think.dom

think.dom helps you to create smart, self-contained, self-initializing, reusable, descriptive UI widgets
JavaScript
7
star
12

lazyflickity

HTML
7
star
13

link-preload

JavaScript
7
star
14

scroll-perf

HTML
3
star
15

tutorial_review_tmp

3
star
16

layout-thrashing

Slides for the Talk "Avoid Layout Thrashing - The easy way."
CSS
2
star
17

foo

1
star
18

respimg-load

HTML
1
star
19

tmp-webpack

JavaScript
1
star
20

portfolio_help

Please help afarkas!
HTML
1
star
21

validation-rules

JavaScript
1
star