admin管理员组文章数量:1315968
I'm trying to implement long polling using Netty and jQuery.
I have it working correctly with Chrome and Firefox, but Internet Explorer 8 is causing me problems.
I'm executing the following code which sends a request to my server, waits until a response is received from the server and then sends another request.
function longPollRequest() {
$.ajax({
url: '/test-path',
type: 'GET',
success: function(data, textStatus, jqXHR) {
longPollRequest();
console.log('Received: ' + data);
}
});
}
However, in IE8 I'm running into an infinite loop, which is freezing the browser. The interesting part is that my server is only receiving the first request from IE. I'm really puzzled as to what is going on. If anyone has any ideas I would really appreciate the help.
I'm trying to implement long polling using Netty and jQuery.
I have it working correctly with Chrome and Firefox, but Internet Explorer 8 is causing me problems.
I'm executing the following code which sends a request to my server, waits until a response is received from the server and then sends another request.
function longPollRequest() {
$.ajax({
url: '/test-path',
type: 'GET',
success: function(data, textStatus, jqXHR) {
longPollRequest();
console.log('Received: ' + data);
}
});
}
However, in IE8 I'm running into an infinite loop, which is freezing the browser. The interesting part is that my server is only receiving the first request from IE. I'm really puzzled as to what is going on. If anyone has any ideas I would really appreciate the help.
Share Improve this question edited Feb 7, 2012 at 1:17 Brian DiCasa asked Feb 6, 2012 at 23:57 Brian DiCasaBrian DiCasa 9,48718 gold badges70 silver badges97 bronze badges 3-
1
The first line should be
function longPollRequest() {
instead. Is that just a typo in your post? – Jacob Commented Feb 7, 2012 at 0:04 - 3 I bet IE8 caches your request: Have your tried `url: '/test-path?nocache='+(Math.random*900000+100000).toString() – Eugen Rieck Commented Feb 7, 2012 at 0:09
- @Jocob Ya that was just a typo. Fixed. – Brian DiCasa Commented Feb 7, 2012 at 1:17
1 Answer
Reset to default 10Disable caching and see if that fixes your issue:
function longPollRequest () {
$.ajax({
url : '/test-path',
type : 'GET',
cache : false,
success : function(data, textStatus, jqXHR) {
longPollRequest();
console.log('Received: ' + data);
}
});
}
This will force jQuery to append a time-stamp to each request. If the response is cached then it will return very quickly and there's a good chance that's what's causing your infinite loop.
You could also force a minimum delay between AJAX requests:
let lastRequestTime = 0;
function longPollRequest () {
lastRequestTime = new Date().getTime();
$.ajax({
url : '/test-path',
type : 'GET',
cache : false,
success : function(data, textStatus, jqXHR) {
let delay = ((new Date().getTime()) - lastRequestTime);
if (delay > 1000) {
delay = 0;
} else {
delay = (1000 - delay);
}
setTimeout(longPollRequest, delay);
console.log('Received: ' + data);
}
});
}
This checks the current time against the time of the last AJAX request. If it's more than one second then just run the function again without a delay, otherwise make the code wait until a second has gone by between requests. There is probably a more elegant way of defining the delay
variable but the above code should get you started.
本文标签: javascriptLong PollingProblems with Internet Explorer 8Stack Overflow
版权声明:本文标题:javascript - Long Polling - Problems with Internet Explorer 8 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741995953a2410047.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论