我的項目基本上就像是實時更新的Reddit源。我試圖使用AJAX每隔一段時間輪詢服務器以更新15個項目。AJAX輪詢和循環
我寫了一個for循環,但它導致瀏覽器鎖定(我猜太多XHR?)。
如何在不鎖定瀏覽器的情況下輪詢Reddit-esque訂閱源上的每個項目?什麼是最有效的方法來做到這一點?
如果有100多個客戶端同時使用Web應用程序,我應該使用長輪詢嗎?或者我應該選擇智能輪詢(如果沒有數據,增加請求之間的等待時間)?
謝謝!我還是AJAX新手!
for (var i=0; i < id_array_len; i++) {
// Grab current reply count
var reply = $("#repl"+item_id).html();
var url= *php function here*
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser does not support AJAX!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if (ajaxRequest.readystate == 4){
live_feed_data_tot = ajaxRequest.responseText;
if (live_feed_data_tot.trim() == "no change" || live_feed_data_tot.trim() == "no meme" || live_feed_data_tot.trim() == "no response"){
console.log("(no update)");
} else {
var live_feed_data = live_feed_data_tot.split(',');
if (live_feed_data[1] == 'reply') {
// Reply count has changed
new_reply = live_feed_data[0].trim();
// Update actual number
$("#repl"+item_id).html(new_reply);
}
}
}
}
ajaxRequest.open('POST', url, true);
ajaxRequest.send();
向我們展示循環。我已經完成了這幾個不同的時間沒有鎖定瀏覽器,所以我期望它是你的代碼中的東西。 – 2012-03-21 19:17:55
添加了代碼 - 幫助將非常感謝! – 2012-03-21 19:29:36