Long polls - issues with Internet Explorer 8

I am trying to implement a lengthy survey using Netty and jQuery.

I work correctly with Chrome and Firefox, but Internet Explorer 8 is causing me problems.

I execute the following code that sends a request to my server, waits for a response 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 run an endless loop that freezes the browser. Interestingly, my server receives only the first request from IE. I am really puzzled by what is happening. If anyone has any ideas, I would really appreciate help.

+6
source share
1 answer

Disable caching and see if this fixes your problem:

 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 add a timestamp to each query. If the response is cached, it will return very quickly and there is a good chance of causing an infinite loop.

You can also set a minimum delay between AJAX requests:

 var lastRequestTime = 0; function longPollRequest () { $.ajax({ url : '/test-path', type : 'GET', cache : false, success : function(data, textStatus, jqXHR) { var 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 for the time of the last AJAX request. If it is more than one second, then just run the function again without delay, otherwise, do the code until the second passes between requests. Probably a more elegant way to define a delay variable, but the code above should get started.

+9
source

Source: https://habr.com/ru/post/907806/


All Articles