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.
source share