问题
i' tring to make chat like facebook chat :)
i'm using this requests like
Here To Get Room Users And roomBody
$('.room_users,.room_body').each(function () {
var page = $(this).attr("page");
var room_id = $(this).parents('.room').children('.roomy_id').attr("value") ;
var url = page+room_id ;
window.setInterval(function () { $(this).load(url);}, 200);
});
Here To Get Room Lists
$('#room_list').each(function () {
var page = $(this).attr("page");
var url = page ;
window.setInterval(function () {
$(this).load(url);
}, 60000);
});
as you see my requests send every 1 second but not all requests return 202 status
many time it return 404 notfound
And Some time request send twice every 1 sec

回答1:
If you server supports websockets or any form of Comet like long polling, try to utilize one. In the mean time, add a timeout to your request AND only send the next ajax request AFTER the previous has returned or timed out...
function updaterooms() {
$.ajax({
type: "GET",
url: page,
async: true,
cache: false,
timeout:5000,
success: function(data){
// do what you need with the returned data...
setTimeout('updaterooms()',1000);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
$("#error").text("ERROR: " + textStatus + " (" + errorThrown + ")");
setTimeout('updaterooms()',1000);
}
});
}
回答2:
Instead of spamming the server with requests every second you should look at Comet.
It is a pattern that uses one long running request to stream data to the browser for a very long time in short intervals.
来源:https://stackoverflow.com/questions/9316690/send-multi-ajax-requests-every-1-second