Skip to content

Instantly share code, notes, and snippets.

@jmuzsik
Created July 30, 2019 04:22
Show Gist options
  • Save jmuzsik/d689cd518cd5633f00b6fa28c7f3862a to your computer and use it in GitHub Desktop.
Save jmuzsik/d689cd518cd5633f00b6fa28c7f3862a to your computer and use it in GitHub Desktop.
// requestIdleCallback's callback.
runTaskQueue(deadline) {
console.info(`This round of JS begins at ${this.currentTaskNumber}.`);
while (deadline.timeRemaining() > 0 && this.taskList.length) {
// deadline.timeRemaining() runs repeatedly, until it is equal
// to 0. It slowly decreases... 12.465 -> 11.26 -> etc.
const task = this.taskList.shift();
this.currentTaskNumber++;
// This creates the DOM elements but does not log them.
task.handler(task.data);
// This adds the created elements to the DOM with requestAnimationFrame.
this.scheduleStatusRefresh();
}
console.info(`And ends at ${this.currentTaskNumber}.`);
// Then run the next task.
if (this.taskList.length) {
this.taskHandle = requestIdleCallback(this.runTaskQueue);
} else {
this.taskHandle = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment