Skip to content

A simple trick to slice long-run task & run it in background

Notifications You must be signed in to change notification settings

felixyoungzZ/time-slicing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Time Slicing

A simple trick to slice long-run task and run it in background.

Example

Sync way:

const button = document.querySelector('#button');

document.addEventListener('mousemove', ({ pageX, pageY }) => {
  button.style.top = `${pageY}px`;
  button.style.left = `${pageX}px`;
});

setTimeout(() => {
  for(let i = 0; i < 1000000; i++) {
    button.innerHTML = i;
  }
}, 1000);

"Async" way:

const button = document.querySelector('#button');

document.addEventListener('mousemove', ({ pageX, pageY }) => {
  button.style.top = `${pageY}px`;
  button.style.left = `${pageX}px`;
})

window.requestIdleCallback(
  timeSlicing(
    // 这里相当于把这个 for 循环分割成了 1000000 个任务
    function*() {
      for(let i = 0; i < 1000000; i++) {
        yield;
        button.innerHTML = i;
      }
    }
  )
);

About

A simple trick to slice long-run task & run it in background

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published