firebug (1.10.1) suggests javascript is not confined to a single thread in firefox (13.0)

前端 未结 3 795
攒了一身酷
攒了一身酷 2021-01-22 00:46

While debugging some client side javascript today in Firefox, I ran into something that I found quite odd and little unnerving. Also, I was unable to duplicate this behavior wh

3条回答
  •  無奈伤痛
    2021-01-22 01:12

    I think it's safe to assume that the anomaly you're observing is caused by how Firebug implements breakpoints/works under the hood. I can't confirm that though. This also happens with FF 14 on OS X.

    Unless jQuery immediately executes your fail() function and surpasses the whole XMLHttpRequest object, then there is a guarantee that the ordering of the statements will be this will probably happen first. then this will probably happen second..

    Given the single threaded nature of JavaScript, functions will be essentially atomic; they will not get interrupted by a callback.

    It seems as though you're trying to simulate what would happen if the click function takes a while to finish executing after calling loadStuff(). The click function shouldn't get interrupted by the fail method executing (mind == blown that you found a way to make that happen).

    To take breakpoints out of the equation, here's a modified version. The rest of the markup is unchanged.

    $(function () {
        $(".test-trigger").on("click", function () {
            loadStuff();
            for (var i = 0; i < 1000000000; i++)
            {
                //block for some interesting calculation or something
            }
            console && console.log && console.log("this will probably happen first.");
        });
    });
    
    function loadStuff() {
        $.get("http://google.com/")
            .fail(function () {
                console && console.log && console.log("this will probably happen second.");
            });
        }
    

    The click function clearly takes a long time to execute, after calling loadStuff(), but the console will still reflect the correct order of the log statements here. Also worth noting, if you insert the same breakpoints, the ordering will be invalid, like the original example.

    I'd file an issue for this with Firebug.

提交回复
热议问题