0

I need to perform semi-continuous AJAX requests to display data based on the latest entry into a DB. This all works fine with a setInterval() but now I notice the continuously increasing number of resources and size in the Web Inspector (see image). I imagine that this may become an issue if the app is open for long periods of time? Or is the size displayed (1) merely network activity? How could I prevent this? I have set the jQuery ajax cache to false.

Web Inspector showing (1) increasing number of resources and size and (2) XHR's.

Update: Did not post any code because there's nothing special there. Just a basic jQuery ajax function, php script that queries DB based on data from the ajax function and echoes it back in a response.

So is the number of KB in the Web Inspector (1) network traffic or cached?

$(document).ready(function(){
    setInterval(refresh, 2000); 
})

function refresh(){
$.ajax({        
    type: "POST",
    cache: false,
    url: "../update.php",
    data: dataString,
    success: function(msg){
        if(msg2 == 'same'){
            // do nothing
        }else{
            $('#result').html(msg);
        }
    }
})
}
Jorgos
  • 123
  • 1
  • 11
  • 2
    without seeing the actual code, there's no way to answer your questions. Just like with anything, there's a right way to do it, and many wrong ways. – Jaromanda X Jul 22 '15 at 13:36
  • Cannot you use websockets for that? If not, why don't you think about `setTimeout` instead `setInterval` and modifying the interval if you don't get new results. For example, if you ask the server for results and you don't get any, you increment your wait time to the double, then you ask again and the interval will increment each time you don't get results. Once you get, you restart your interval time. At least, it will mean less calls to the server. – Mindastic Jul 22 '15 at 13:38
  • You can also add an event listener for clicks, for example, or for scroll, and store a timestamp each time it happens and, before running your call to the backend, you check if the maximum time has exceeded and, if so, you clear the timeout/interval. – Mindastic Jul 22 '15 at 13:39
  • Good point about changing it to setInterval, @Mindastic . Could also place it under the 'complete' callback, not 'success'... – Jorgos Jul 22 '15 at 13:50

0 Answers0