2012-02-16 153 views
1

我特林让聊天像Facebook聊天:)多发送Ajax请求每隔1秒

我使用的这个要求,是

这里获取房用户和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状态

很多时候它返回404未发现

还有一些时间请求发送两次,每1秒

Here is pic of this

回答

1

如果您的服务器支持websockets或任何形式的Comet(如长轮询),请尝试使用它。与此同时,增加一个超时请求,只发送下一个ajax请求之后,先前已经返回或超时...

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); 
    } 
    }); 
} 
0

相反垃圾邮件与请求的服务器每秒你应该看看Comet的。

这是一种模式,它使用一个长时间运行的请求在很短的时间间隔内将数据流式传输到浏览器很长一段时间。