Last active
May 12, 2017 05:58
-
-
Save crongro/5ccc98cf8b974abce3bd6a0ced86031a to your computer and use it in GitHub Desktop.
requestidlecallback
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 from "https://developer.mozilla.org/en-US/docs/Web/API/Background_Tasks_API" | |
window.requestIdleCallbackCustom = function(handler) { | |
let startTime = Date.now(); | |
return setTimeout(function() { | |
handler({ | |
didTimeout: false, | |
timeRemaining: function() { | |
return Math.max(0, 50.0 - (Date.now() - startTime)); | |
} | |
}); | |
}, 1); | |
} | |
requestIdleCallbackCustom(function myhandler(obj) { | |
let result = 0; | |
for(let i=0; i<999999; i++) {result += Math.random()} | |
let timeremain = obj.timeRemaining(); | |
console.log("1=>", timeremain); | |
if(timeremain < 10) return; | |
requestIdleCallbackCustom(function myhandler(obj) { | |
let result = 0; | |
for(let i=0; i<100; i++) {result += Math.random()} | |
let timeremain = obj.timeRemaining(); | |
console.log("2=>", timeremain); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment