Ajax polling crashing the browser as its memory usage,cpu utilization continously increasing ? Any alternative

StackOverflow https://stackoverflow.com/questions/15815221

Question

I am new to the ajax polling and i implemented to fetch data continuously , But the problem i am getting is Memory usage and CPU utilization is continously keep on increasing and in the last the browser is crashing . Here is ajax call what i am using to fetch data continuously .

$(document).ready(function () {

make_call();
function make_call() {
$.ajax({
url: "url",
              accepts: "application/json",
              cache: false,
              success: function (result) { // Some code here },
complete: make_call
});
}
}

Is there any other alternative , or am i doing something wrong . Please provide some suggestion or solution . Thanks in advance .

Was it helpful?

Solution

Your code initializes a new request at the same moment the previous requests completes (complete being either an error or success). You likely want to have a small delay before requesting new data - with the benefit of reducing both server and client load.

$.ajax({
  // ...
  complete: function() {
    setTimeout(make_call, 5000);
  }
});

The above code waits for 5 seconds before making the next request. Tune the value to your needs of "continuous".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top