中断JavaScript函数执行

时间:2013-04-05 13:52:54

标签: javascript

我有javascript的html页面:

<!DOCTYPE html>

    <html>

        <head>
            <script>
                function doWork() {
                     console.log("\tdoWork-start");
                     var now = new Date();
                     var end = now.getTime() + 2000;
                     while(now < end) {
                        now = new Date();
                     }
                     console.log("\tdoWork-end");
                }

                function handleOnClick() {
                    console.log("onclick-start");
                    console.log("onclick-end");
                }

                function handleOnchange() {
                    console.log("onchange-start");
                    window.open('', 'name', 'height=300,width=600,top=200,left=200');
                    doWork();
                    console.log("onchange-end");
                }
            </script>
        </head>

        <body>
            <form>
                <input type="text" onchange="handleOnchange();">
                <input type="button" value="ok" onclick="handleOnClick();">
            </form>

        </body>

    </html>

使用案例:在输入控件中输入一些值并快速单击按钮。 我希望这些日志在控制台中:

onchange-start
    doWork-start
    doWork-end
onchange-end
onclick-start
onclick-end

但在Firefox中我得到了:

onchange-start
onclick-start
onclick-end
    doWork-start
    doWork-end
onchange-end

注意,onclick执行会在更改执行时中断。函数doWork模拟了一些长期工作。 在Chrome中,它按预期工作。

有人可以解释这是怎么可能的吗?

0 个答案:

没有答案
相关问题