本文介绍了发送多Ajax请求每隔1秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我'特林让聊天像Facebook聊天:)
i' tring to make chat like facebook chat :)
我使用的这个要求像
这里获取房用户和roomBody
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);
});
当你看到我的请求发送每隔1秒,但并非所有的请求将返回202状态
as you see my requests send every 1 second but not all requests return 202 status
很多时候它返回404 NOTFOUND
many time it return 404 notfound
和一些时间请求发送两次,每1秒
And Some time request send twice every 1 sec
推荐答案
如果你的服务器支持的WebSockets或任何类似长轮询形式的彗星,试图利用一个。在平均时间,超时添加到您的请求,只发送下一个AJAX请求后,previous已恢复或超时...
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);
}
});
}
这篇关于发送多Ajax请求每隔1秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!