0

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 .

user1260967
  • 89
  • 1
  • 2
  • 14
  • browsers usually only allocate 1 thread to js activity per page, so your script is leaving no time for any other js activity to happen or any resource cleanup to happen. i would use window.settimeout to chain up the make_call functions with about 1500ms inbetween each call – Slicksim Apr 04 '13 at 15:31
  • yup i tried with that too...still the memory usage keep on increasing .....any way to restrict this? thnx for comment – user1260967 Apr 04 '13 at 15:57

1 Answers1

0

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".

bjorne
  • 61
  • 2
  • thnxs for answer,i tried in that way too but still my browser is crashing ....as i mentioned earlier its memory usage,cpu utilization keep on increasing...is there anyway to restrict these things..so that browser not crash ....or any alternative to ajax pooling ??? – user1260967 Apr 04 '13 at 15:55
  • If memory and CPU utilization keeps increasing it is probably related to how the result is handled. You should probably look for memory leaks within that piece of code. A better alternative would be WebSockets or Server-sent Events - but it will probably not help the problem. – bjorne Apr 04 '13 at 16:50
  • earlier i tried like complete : make_call , timeout : 5000 afterwards i changed as per above comment , I feel there is difference in both of these statements , now the memory usage is increasing at the slow rate , i also keep my variables value as null after each iteration and keeping cache : false , but is it possible to restrict the increase of memory usage ??? any further ideas .....thnxs for your comment – user1260967 Apr 05 '13 at 06:19
  • The `timeout` and `cache` options for the `$.ajax` will not affect memory usage (significantly) - they only affect how the AJAX request is made. Setting variables to `null` after each request sounds reasonable, but unless you show the code for (or describe more in detail) how you handle the result it will be hard to give good advice. – bjorne Apr 05 '13 at 09:31