Skip to content

Latest commit

 

History

History
50 lines (38 loc) · 921 Bytes

15-what-worked.md

File metadata and controls

50 lines (38 loc) · 921 Bytes

What worked, what did not?

Trick: requestAnimationFrame + setTimeout, a simple polyfill for requestPostAnimationFrame.

Answer
function afterNextPaint(callback) {
  requestAnimationFrame(() => {
    setTimeout(callback, 0);
  });
}

button.addEventListener("click", () => {
  score.incrementAndUpdateUI();

  afterNextPaint(() => {
    blockFor(1000);
  });
});

Alternatively:

Answer
function afterNextPaint(callback) {
  requestAnimationFrame(() => {
    setTimeout(callback, 0);
  });
}

async function nextPaint() {
  return new Promise(resolve => afterNextPaint(resolve));
}

button.addEventListener("click", async () => {
  score.incrementAndUpdateUI();

  await nextPaint();
  blockFor(1000);
});

Next: Mid-point summary