Search code examples
javascripthtmlfirebug

Firebug debugger ignore setTimeout? How to test it?


Windows 7
Firefox 10.0.1

I have simple setTimeout logic that I want to test using Firebug debugger with few breakpoints.

test.js

console.log("one");   
setTimeout(function(){ console.log("hmm"); }, 5000);
console.log("two");   //breakpoint here
console.log("three");

test.html

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>

        <script type="text/javascript" src="../js/test.js" ></script>

    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

Using Firebug debugger, I put breakpoint in the commented line (where console.log("two") ) and when I load the html page I get output:

one

and process halt at the breakpoint as expected but setTimeout never gets executed.

Is this firebug debugger bug? Has anyone seen this before?

UPDATE:

I investigate bit more and found:

If you click on "continue" during debugger mode and go through breakpoints within 5000ms (which is specified time in setTimeout), the "hmm" msg shows up.

If you halt at breakpoint for more than 5000ms then "hmm" msg won't show up even after "continue" button.


Solution

  • setTimeout triggers only after Javascript execution thread becomes free, even if the time is out while thread is busy. Putting a break point just suspends the thread and it stays busy while stoppend on it. So, this code will return

    one
    two 
    three
    hmmm //(in 5 seconds, if no breakpoint, at once if you stop at a breakpoint for more than 5 seconds and then run the code further)
    

    This is the correct setTimeout behaviour.