Reputation: 36654
For a dynamic page, I use Ajax Long Polling and even with jQuery 1.9, Internet Explorer hangs after the first request.
The script code is based on the article Simple Long Polling Example with JavaScript and jQuery
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
(function poll(){
$.ajax({ url: "ajaxstats.json", success: function(data){
$("button.requests" ).empty().append(data.requests);
}, dataType: "json", complete: poll, timeout: 30000 });
})();
});
</script>
The console shows no errors.
The IE network monitor immediately shows many requests to the ajaxstats.json
resource with a response time of < 1 ms and a 304 (not modified)
response code. The response body is correct (JSON code).
The server code always delays the answer by 1000 milliseconds. And in Firefox, Firebug XHR log shows that every request takes around 1000 milliseconds, as expected.
The HTTP response code is different between Firefox and Internet Explorer:
200 ok
304 (not modified)
Is there a way to work around this IE problem?
Upvotes: 5
Views: 4886
Reputation: 91
I don't have a good answer on why IE9 hangs other than the fact that IE9 is just slow. It will intermittently take forever to call the callback of an ajax call. Testing at work, I've seen the same ajax call against the same server take more than 5x the time in IE9 as it does in Firefox, even though the browsers are running on the same machine.
If you are building a real time app and have access to the actual server it is running on I highly recommend you use Socket.IO. http://socket.io/ Originally it was built for node.js but there are server side implements now for most of the major languages.
The client library has this fall back order:
On the newer browsers you get true web socket performance, on those that don't support it you get long polling for free but you get to just treat it as a web socket using the same clean Socket.IO interface.
Upvotes: 1
Reputation: 2942
Try setting cache param to false, if set to false, it will force requested pages not to be cached by the browser.
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
(function poll(){
$.ajax({ url: "ajaxstats.json", success: function(data){
$("button.requests" ).empty().append(data.requests);
}, dataType: "json", complete: poll, timeout: 30000, cache: false });
})();
});
</script>
Upvotes: 7
Reputation: 5767
Use the setTimeout
version on the article. The timeout
option sets the timeout for the request, not the time to wait until the next request.
There's a reply to a comment from Lars, where the author suggests that.
Upvotes: 3