簡體   English   中英

有時,javascript是多線程的嗎?

[英]sometimes, javascript is multithread?

我發現有時候,JavaScript有多個線程,這讓我很煩。 我檢查了一下網絡,發現人們總是說它只是一個線程,但遇到了多個線程。 您可以將代碼復制到html文件中,然后運行。 您會明白我的意思的。 為什么javascript具有警告,確認和提示多個線程? 謝謝閱讀。

注意:要測試它,您應該獲取並替換它。

<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中有多個線程的證據。 有其他解釋。 但是,我確實同意您的困惑,因為我同意至少在一種流行的瀏覽器中存在令人困惑的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM