Skip to content

Instantly share code, notes, and snippets.

@crongro
Last active May 15, 2017 05:46
Show Gist options
  • Save crongro/a8bc6b64aded061c6d31d53aa58be055 to your computer and use it in GitHub Desktop.
Save crongro/a8bc6b64aded061c6d31d53aa58be055 to your computer and use it in GitHub Desktop.
requestIdleCallback example
//Forked -> https://developers.google.com/web/updates/2015/08/using-requestidlecallback
var tasks = [1,2,3,4];
function myNonEssentialWork (deadline) {
console.log("myNonEssentialWork called", deadline.timeRemaining());
let result="";
while (deadline.timeRemaining() > 0 && tasks.length > 1) {
for(let i=0; i<999999; i++) {result += Math.random()}
console.log("do work ..", tasks);
tasks.pop();
}
if (tasks.length > 1)
requestIdleCallback(myNonEssentialWork, { timeout: 50 });
}
requestIdleCallback(myNonEssentialWork, { timeout: 50 });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment