有时,JavaScript是多线程的

sometimes, javascript is multithread?

本文关键字:多线程 JavaScript 有时      更新时间:2023-09-26

我发现有时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>

Javascript 只有一个工作线程,但它有回调,所以在你看来可能有多个线程,但没有

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

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

确实在使用最新版本的Firefox时看到了一些奇怪的东西。也就是说,DOM 似乎在显示警报(或之前)更新(即,传递给 AJAX 请求的回调被触发)。我感觉到您已经得出结论,必须有多个线程,因为alert应该是一个阻塞调用。

我不知道这是事实,但我有一个理论来解释为什么会这样。如果你考虑alert的语义,它要求调用alert的代码在用户单击"确定"(或其他任何内容)之前不会执行。但这实际上并不意味着调用必须阻止传统意义上的(即旋转和等待)。JavaScript 引擎实际上可能将alert实现为基于回调的协议,以便alert调用后的代码构成用户单击"确定"后触发的回调。

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

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

我在这里想说的重点是,你实际上还没有发现JavaScript中多个线程的证明。还有其他解释。但是,我确实同意您对至少一个流行浏览器中的一些令人费解的行为感到困惑。