有时候,javascript是多线程的吗?

时间:2012-06-18 03:24:24

标签: javascript multithreading alert confirm

我发现有时候,JavaScript有多个帖子,这真让我恼火。我检查了网络,我发现人们总是说它只是一个线程,但我遇到了不止一个。你可以复制放入html文件的代码,然后运行。 你会看到我的意思。为什么javascript有多个线程有警报,确认,提示?谢谢你的阅读。

注意:要测试它,你应该得到你的jQuery并替换它。

<html>
<head>
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
function pauseWithAlert(){
  fireAjax();
  alert("pause at one thread, I guess before this OK is clicked, no javascript should be executed since single thread, and the runner is with this thread in this function fpause");
}

function pauseWithConfirm(){
  fireAjax();
  if(confirm("the message has been triggered, while the 1st thread is paused with this confirmation request, but the message from another thread has been presented to you")){
    //something;
  }
}

function pauseWithDeadLoop(){
  alert("after you click OK, I will trigger the message, and then let the 1st thread get into dead loop, you will not be able to see the message");
  fireAjax();
  while(true);
}


function fireAjax(){
  var location = document.location.toString();
  $.ajax({type:'get',dataType:'html',url:location,success: ajaxSuccess, error: ajaxError});
}

var message = "have you clicked the OK in the 1st thread, this is a message from 2nd thread, isn't it?";
function ajaxSuccess(){
  document.getElementById('ajaxmessage').textContent = message;  
}

function ajaxError(){
  document.getElementById('ajaxmessage').textContent = message;  
}

function clearMessage(){
  document.getElementById('ajaxmessage').textContent = "";  
}
</script>
</head>
<body>
I know that it was said javascript is just one thread.
However, I found that sometimes, it is not just one thread.
<br/>Click <span onclick="pauseWithAlert();">here to pause with an alert</span> or
  <span onclick="pauseWithConfirm();">here to pause with a confirm</span>, 
and the runner stops there in the 1st thread, but you will see a message from 
another thread below, so not single thread????? No!!!
<br/>Click <span onclick="pauseWithDeadLoop();">here</span> to stop the 1st thread 
with a dead loop, you will not see a message from another thread below.
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>

<span id=ajaxmessage></span>
<br/>
<span onclick="clearMessage();">Clear the above message</span>

</body>
</html>

2 个答案:

答案 0 :(得分:7)

Javascript只有一个工作线程,但它有回调,所以你可能觉得有多个线程,但没有。

答案 1 :(得分:0)

简短的回答是,这取决于浏览器。但它实际上并没有演示多个线程。

例如,使用最新版本的Chrome,我看不到您所描述的行为。

使用最新版本的Firefox查看奇怪的内容。即,DOM似乎更新(即,传递给AJAX请求的回调被触发),而(或之前)显示警报。我觉得你已经得出结论必须有多个线程,因为alert应该是一个阻塞调用。

我不知道这个事实,但我有一个理论为什么会出现这种情况。如果您考虑alert的语义,则需要在 alert之后调用代码才会执行,直到用户单击“确定”(或其他)为止。但这并不意味着呼叫必须在传统意义上阻止(即旋转和等待)。 JavaScript引擎可能实际上将alert实现为基于回调的协议,以便alert调用后的代码构成在用户单击“确定”后触发的回调。

根据此实现,调用$.ajax后跟alert就像连续两次$.ajax次调用一样,在这种情况下,第一次调用就不足为奇了在第二个AJAX请求收到响应之前,回调可能会触发。

另一种可能性(尚未测试)是Firefox只有一个优化,其中对当前窗口位置的AJAX请求同步返回当前页面的HTML。也许是牵强附会,但肯定是可能的。

我想在这里提出的要点是你还没有真正发现JavaScript中多线程的证据。还有其他解释。但是,我确实对你在至少一个流行的浏览器中的一些令人费解的行为感到困惑。