-1

JavaScriptには複数のスレッドがあることが時々ありましたが、これは本当にイライラしました。ウェブをチェックしたところ、いつも1つのスレッドだと言われていましたが、複数のスレッドに遭遇しました。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>
4

2 に答える 2

7

Javascript には作業スレッドが 1 つしかありませんが、コールバックがあるため、複数のスレッドがあるように見えるかもしれませんが、スレッドはありません。

于 2012-06-18T03:29:23.340 に答える
0

簡単に言えば、これはブラウザに依存するということです。ただし、実際には複数のスレッドを示しているわけではありません。

たとえば、最新バージョンの Chrome を使用すると、あなたが説明している動作が見られません。

最新バージョンの Firefox を使用すると、奇妙なことがわかります。つまり、アラートが表示されている(または表示される)に、DOM が更新されたように見えます (つまり、AJAX 要求に渡されたコールバックが起動されました)。alertブロッキング呼び出しであるはずなので、複数のスレッドが必要であると結論付けたようです。

事実はわかりませんが、なぜそうなのかについては 1 つの理論があります。のセマンティクスについて考えると、が呼び出された後alertのコードは、ユーザーが [OK] (またはその他) をクリックするまで実行されないことが必要です。しかし、これは実際には、従来の意味 (つまり、スピンして待機) で呼び出しがブロックされなければならないという意味ではありません。JavaScript エンジンは実際にはコールバック ベースのプロトコルとして実装され、呼び出し後のコードが、ユーザーが [OK] をクリックした後に起動されるコールバックを構成する場合があります。 alertalertalert

この実装によれば、呼び出し$.ajaxに続いて呼び出しを行うことはalert、2 つの$.ajax呼び出しを連続して行うのとまったく同じです。この場合、2 番目の AJAX 要求が応答を受信する前に最初のコールバックが発生してもまったく驚くことではありません。

もう 1 つの可能性 (テストしていません) は、Firefox が現在のウィンドウの場所への AJAX 要求が同期的に現在のページの HTML を返す最適化を行っているだけです。おそらく、とてつもないことですが、確かに可能です。

私がここで強調したいのは、JavaScript のマルチスレッドの証拠を実際に明らかにしていないということです。代わりの説明があります。ただし、少なくとも 1 つの一般的なブラウザーで不可解な動作が発生することに同意することについて、あなたの混乱を共有します。

于 2012-06-18T04:13:14.143 に答える