Boss lesson · Unit 03
A frozen page
One slow click handler: why the page freezes, and three ways to give the thread back
What you practise
- A long task freezes input, splitting work,
requestAnimationFrame, web workers, where script loading fits
Try a question from this lesson
Pick one
A page filters 50,000 rows when you click Filter, in one loop that takes 3 seconds. During those 3 seconds you click the menu. What happens to that click?
Correct answer
It waits in the task queue until the loop ends
Why
A click is a task, and the loop takes the next task only when the stack is empty. Nothing interrupts running JavaScript. The click is kept and handled 3 seconds late, which to the user looks like a dead page.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Long tasks
The main thread runs your JavaScript, and it also does style, layout and paint and answers every click and key press.
A task that holds it for more than about 50 ms is called a long task. For as long as it runs, the page is a still picture: no updates, no hover, no typing.
Concept
requestAnimationFrame
Most screens are painted 60 times a second. A change made between two paints is only seen at the next one.
requestAnimationFrame(fn) runs fn once, just before the next paint. It is the right place for anything visual: one update per frame, never more.
10 more exercises in the app
- 4× multiple choice
- 1× fill the blanks
- 3× type the answer
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.