Skip to content

Instantly share code, notes, and snippets.

@crongro
Last active May 12, 2017 05:58
Show Gist options
  • Save crongro/5ccc98cf8b974abce3bd6a0ced86031a to your computer and use it in GitHub Desktop.
Save crongro/5ccc98cf8b974abce3bd6a0ced86031a to your computer and use it in GitHub Desktop.
requestidlecallback
//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