有时,JavaScript是多线程的
sometimes, javascript is multithread?
我发现有时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中多个线程的证明。还有其他解释。但是,我确实同意您对至少一个流行浏览器中的一些令人费解的行为感到困惑。
- javascript中的多线程
- Javascript支持多线程吗
- 为什么Javascript SetTimeout()不是多线程的
- 从多线程C++插件回调NodeJS Javascript函数
- 如何使用并行/多线程Hamsters.js Javascript库编辑数组中的对象属性
- 是否可以使用多线程执行Javascript函数
- 使用不同选项卡的Javascript中的多线程
- JavaScript 回调 - 多线程
- 有时,JavaScript是多线程的
- Javascript 多线程两个窗口焦点
- JavaScript 中的多线程
- 既然JavaScript是单线程的,那么HTML5中的web工作者是如何进行多线程的呢
- Delphi-can't在多线程应用程序中使用IWebBrowser2来抑制JavaScript错误对话框
- JavaScript:如何防止setIntegal触发多线程
- Javascript引擎(或其他可嵌入语言),用于类似mongodb的查询执行环境和多线程
- Javascript -定期向服务器发送用户位置数据(多线程)
- Alfresco JavaScript/Rhino多线程处理和并发
- ActiveX多线程调用javascript回调例程出现问题
- 要多线程还是不要多线程- JavaScript
- 我们真的需要多线程JavaScript吗?