Firefox:JavaScript运行时没有点击事件

时间:2016-12-25 18:47:31

标签: javascript asynchronous events event-handling

这是我的html文件:

<!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>

由于EventHandler在JavaScript等待3秒之前已注册,因此即使我在这3秒内点击,它也应该在最后记录"Click"

但是,当我点击 "Click"后的时,它只会记录"End Global Execution Context"

2 个答案:

答案 0 :(得分:0)

显然我的安装已损坏。新安装解决了该问题。

答案 1 :(得分:-3)

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

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


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