Skip to content

Instantly share code, notes, and snippets.

@cyan33
Last active May 24, 2018 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyan33/c4395aabc740ea7b973faf81f0f5263f to your computer and use it in GitHub Desktop.
Save cyan33/c4395aabc740ea7b973faf81f0f5263f to your computer and use it in GitHub Desktop.
requestIdleCallback
// cannot force call the callback when it times out though
var requestIdleCallback = window.requestIdleCallback || function(callback, {timeout = Infinity}) {
let startTime = Date.now();
return setTimeout(function() {
// 50 -> duration per frame
var timeRemaining = Math.max(0, 50 - (Date.now() - startTime));
callback({
didTimeout: timeRemaining === 0,
timeRemaining,
});
}, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment