我有一段AJAX的語法
var http_request = false;
function send_request(obj,url)
{
http_request = false;
if(window.XMLHttpRequest)
{
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject)
{
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
}
}
}
if (!http_request)
{
obj.innerHTML="不能創建XMLHttpRequest.";
return false;
}
http_request.onreadystatechange = function(){processRequest1(obj)};
http_request.open("GET",url, true);
http_request.send(null);
}
function processRequest1(obj)
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
obj.innerHTML=unescape(http_request.responseText);
}
else
{
obj.innerHTML="錯誤";
}
}
else
{
obj.innerHTML="<IMG SRC=../IMAGES/LOADER6.GIF>";
}
}
在同一個網頁裡,有設定TIMER,讓多個DIV的資料可以透過這個JAVASCRIPT更新資料,但是發現,只要同時運作,就跑不出來,間隔時間運作就可以,我該怎樣修改,讓資料可以同時進行更新
謝謝大家
http://fillano.blog.ithome.com.tw/,部過許久未更新...
有一個觀念需要澄清,網頁中的javascript並不是執行緒,所以不太可能「同時」進行更新。
另外,你的程式共用一個http_request整體變數,所以你不能讓他同時處理多個request。最快的改法是像這樣:
<pre class="c" name="code">
function send_request(obj,url)
{
var http_request = false;
if(window.XMLHttpRequest)
{
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject)
{
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
}
}
}
if (!http_request)
{
obj.innerHTML="不能創建XMLHttpRequest.";
return false;
}
function processRequest1(obj)
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
obj.innerHTML=unescape(http_request.responseText);
}
else
{
obj.innerHTML="錯誤";
}
}
else
{
obj.innerHTML="<IMG SRC=../IMAGES/LOADER6.GIF>";
}
}
http_request.onreadystatechange = function(){processRequest1(obj)};
http_request.open("GET",url, true);
http_request.send(null);
}