MySQL データベースに大量のデータを挿入する XMLHttpRequest があり、5 ~ 10 秒かかります。リクエストのコードは次のとおりです。
function sendRequest(url, params, divToChange)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
var httpRequest = new XMLHttpRequest();
}
else // code for IE6, IE5
{
var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
// Stage 3 - open the request and prepare variables for the PHP method being
activated on server side//
httpRequest.open("POST", url, true);
//Set request headers for POST command//
httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpRequest.setRequestHeader("Content-length", params.length);
httpRequest.setRequestHeader("Connection", "close");
httpRequest.onreadystatechange=function()
{
if(httpRequest.readyState==3)
{
document.getElementById(divToChange).innerHTML='please wait';
}
if(httpRequest.readyState==4 && httpRequest.status==200)
{
document.getElementById(divToChange).innerHTML=httpRequest.responseText;
}
}
httpRequest.send(params); // Stage 4 - sending the request to the server//
//end when the server will responde we will execute the
"document.getElementById("txtHint").innerHTML=xmlhttp.responseText;" //
}
//Calling the request with this function//
function createEventList()
{
var url = "grid-update_event_list.php";
var params = "firstLoad=1";
//Create the httpRequest and send it to the server//
sendRequest(url,params, "eventsList");
}
そして、これは応答テキストを取得するために私のhtmlで定義されています:
<p>Events List Status: <b id='eventsList'> </b></p>
ブラウザの 2 つの動作を理解できません。
1-「しばらくお待ちください」というテキストがブラウザに表示されません。何が起こっているのかをテストするために を追加するalert(httpRequest.readyState)
と、予想どおり「3」のポップアップが表示されましたが、 に移動した直後にreadyState==3
それが見られましたreadyState==4
。それらは同時に起こっているようです。httpRequest.responseText;
of で返されたテキストが表示されますreadyState==4
。
2-リクエストを送信した後、別のページに移動できません。現在のページは応答しています(コンボボックスを変更できます)が、別のリンクに移動しようとすると、ブラウザはreadyState = = 4まで待機し、それからリンクに進みます。リクエストが として定義されているため、それも理解できませんasynchronous = true
。
これらの2つの動作を引き起こす何か間違ったことをしていますか? ありがとう。