DEV Community

Noor ul hassan
Noor ul hassan

Posted on

Why Do Clicks “Stack Up” When JavaScript Blocks the UI?

I recently noticed something interesting while experimenting with JavaScript.

I wrote a simple synchronous loop that runs for about 10 seconds:

const start = Date.now();
while (Date.now() - start < 10000) {
  // Block for 10 seconds
}
Enter fullscreen mode Exit fullscreen mode

During those 10 seconds, the UI completely freezes. That part makes sense.

But here’s the interesting part:

While the loop was running, I kept clicking a button on the page. Nothing happened. No console logs. No UI updates.

Then the loop finished…

And suddenly, all my clicks fired at once.

It felt like the browser had “saved” them somewhere.

So what’s actually happening?


The Short Answer: OS Input Buffering

When JavaScript runs a heavy synchronous task, the main thread (Call Stack) is blocked.

But JavaScript freezing does not mean your entire system freezes.

Let’s break it down:

1️ Hardware Level

When you click your mouse, the hardware sends an interrupt signal to the Operating System.

2️ OS Level

The OS records that click in its input buffer.
Even if your browser tab is busy, the OS is still fully operational.

3️ Browser Level

The browser receives those events from the OS and places them into the Macrotask Queue (also called the Callback Queue).


Why Do They Execute After the Loop?

The Event Loop cannot check the Macrotask Queue until the Call Stack is empty.

During the 10-second loop:

  • The Call Stack is busy.
  • The Event Loop is stuck.
  • It literally cannot move to the next “tick” to process events.

After the loop finishes:

  • The Call Stack clears.
  • The Event Loop runs again.
  • It sees all the queued click events.
  • It processes them one by one.

That’s why everything fires at once.


The Big Insight

JavaScript is single-threaded.

But your Operating System is not.

Even when your JavaScript is frozen, the OS is still collecting user input and handing it to the browser.

The browser politely queues those events until JavaScript is ready again.


Why This Matters

Understanding this helps explain:

  • Why heavy synchronous code freezes the UI
  • Why clicks seem to “stack up”
  • Why long-running tasks should be avoided on the main thread
  • Why Web Workers exist

It also makes the Event Loop feel less magical and more mechanical.

Sometimes weird UI behavior isn’t magic — it’s just how the system is designed.


GIF demonstrates when UI blocked but over events are buffered by OS

Top comments (0)