Last active
May 15, 2017 05:46
-
-
Save crongro/a8bc6b64aded061c6d31d53aa58be055 to your computer and use it in GitHub Desktop.
requestIdleCallback example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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