Block the event loop for a specific amount of time

February 26, 2019

If you want to block / slow down the event loop for a specific amount of time you can utilize a while loop that checks if a certain amount of time has passed in each iteration and will only finish once that has happened.

I used to create astronomically large arrays to slow down execution, but this is much better and much more precise and predictable.

Example

This will block the event loop for 500 milliseconds before continuing.

const endDate = new Date().getTime() + 500; console.time(); // this blocks the event loop while (new Date().getTime() <= endDate) {} // logs something like default: 500.878173828125 ms console.timeEnd();

https://jsfiddle.net/robindrexler/ybuafxsw/2/