2016-12-25 45 views
1

這是我的html文件:Firefox中:沒有點擊事件,而JavaScript的運行

<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
    <script> 
    function wait() { 
    // I know this is obsolete 
    var ms = 3000 + new Date().getTime(); 
    while(ms > new Date()) {} 
    console.log("3 Seconds Are Over"); 
    } 

    function clickHandler() { 
    console.log("Click"); 
    } 

    document.addEventListener("click", clickHandler); 

    wait(); 
    console.log("End Global Execution Context"); 
    </script> 
</body> 
</html> 

如JavaScript等待3秒前的事件處理程序被註冊,就應該登錄「點擊」到了最後,甚至當我點擊在這3秒內。 (我希望你明白我的意思,我的英語技能不是很好 - 如果沒有,請告訴我)。

然而,當我在「結束全局執行上下文」之後單擊時,它只記錄「單擊」。

+0

JS默認爲單線程。根據你正在運行的環境,你可以使用web worker或子進程等並行化。 –

+0

對不起,我誤讀了。是的,事件應該放在隊列中,然後在循環之後觸發;這正是我從你的代碼中得到的。如果您在前3秒內點擊時未收到「點擊」(3秒後),請注意您正在測試的瀏覽器,因爲我無法重現您的發現? – Amadan

+0

@Amadan我在debian上使用Firefox ESR 45.4.0 – Scriptim

回答

-3
function clickHandler() { 
    console.log("Click"); 
    } 

    setTimeout(function(){ 
    document.addEventListener("click", clickHandler); 
    console.log('event asigned') 
    },3000) 


    console.log("End Global Execution Context");