MB
My Go-To Quick Debugging Trick: Freezing the Page with setTimeout and debugger

My Go-To Quick Debugging Trick: Freezing the Page with setTimeout and debugger

December 30, 2023

As a web developer, one of the trickiest things to debug is elements like dropdowns, menus, or lists that tend to disappear or change state when you unfocus or interact with other parts of the page. My favorite and super quick trick to tackle this is using setTimeout(() => { debugger; }, 5000). Here's why it's so effective:

  1. Time to Interact: Setting a delay of 5 seconds (or any time you prefer) gives you enough time to interact with the page and bring the element into the desired state.
  2. Pause Execution: When the debugger statement is hit, the browser's execution pauses, effectively "freezing" the page. This lets you inspect the current state of your elements.
  3. Perfect for Ephemeral Elements: This is especially useful for elements that disappear when you click away or open developer tools, as it allows you to examine their styles, attributes, and more.
  4. No Complex Setup Needed: It's a simple line of code that doesn't require setting up breakpoints in the DevTools or modifying much of your existing code.

Usage:

javascript

setTimeout(() => { debugger; }, 5000)
js

Just run this in the browser console or in your code and interact with your page to get the elements in the state you want to inspect, and wait for the pause execution.

Remember, this is a development-only trick, so ensure to remove it from your production code if you use it in the code!

Happy debugging!