Skip to content

Instantly share code, notes, and snippets.

@Robin-front
Created August 25, 2017 07:28
Show Gist options
  • Select an option

  • Save Robin-front/a4a62de055c6f122b4dddee970aaa84e to your computer and use it in GitHub Desktop.

Select an option

Save Robin-front/a4a62de055c6f122b4dddee970aaa84e to your computer and use it in GitHub Desktop.
function createIdleTask(){
var task = [],
timer = null;
function runTask(deadline){
var len = task.length;
while(deadline.timeRemaining() > 0 && len--) {
task.shift()();
}
if (len > 0){
timer = requestIdleCallback(runTask);
}
}
return {
addTask: function (handle){
task.push(handle);
timer = requestIdleCallback(runTask);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment