2

I use this script to update a div, gathering new information constantly. It works well in every browser, except in Google Chrome where it hangs after a while and the browser turns blue and a small sad graphic tells me there's something wrong. The rest of the browser stays intact, it's only the affected tab so it has to do something with this code:

<script type="text/javascript">
    setInterval("SANAjax();", 500); 
    $(function(){
        SANAjax = function(){
            $("#target").load("page.php");
        }
    });
</script>

Is it not safe to refresh a div that many times? Will it overload or what not?

6
  • 2
    1) Why are you declaring your SANAjax function insider of an anonymous function inside a jQuery call? 2) Updating every 500ms seems a little much, why not up that to 1+ seconds? Aug 12, 2011 at 14:23
  • 1) How would you do it? 2) What is the difference, is it harmful in some way to refresh so much?
    – Paparappa
    Aug 12, 2011 at 14:27
  • Look at 321X's answer. Just like that. As for the refresh... it isn't really harmful, but if you have 1000 people on that page at .5 secs per call... that is potentially 2000 pages requests per second. Aug 12, 2011 at 14:30
  • @Paparappa For a few visitors on your website 500 milliseconds is not a problem. Where there is a pike and there are (lets say) 50 visitors online at your site, does this require 100 connections PER second!
    – 321X
    Aug 12, 2011 at 14:40
  • Okey well i have alot of visitiors at my site. I havent used this script on the real site but it can have around 100-1000 members online all the time, of course not everyone is using this page at the same time. But the risk is out there, if i set it to refresh every 2 seconds, would that spare me risks or should i look for another solution?
    – Paparappa
    Aug 12, 2011 at 14:46

2 Answers 2

5

I would do it a bit different than that.

Untested:

$(function(){
    SANAjax();
});

function SANAjax(){
    $('#target').load('page.php', function(){
        setTimeout(SANAjax, 500);
    });
}

This makes sure a new request is being made after the current one is finished. http://api.jquery.com/load/

8
  • 1
    +1 Words out of my mouth, with polish! I'd just like to note that this method means AJAX requests won't pile up if each one takes longer than (in this case) 500ms; the next one is only called after the previous is complete.
    – Bojangles
    Aug 12, 2011 at 14:27
  • 1) Why would you do different? 2) As most untested code it doesent work :P
    – Paparappa
    Aug 12, 2011 at 14:30
  • Let's say that your webserver is pretty busy and 1 request takes up to 500 milliseconds. What do you think is going to happen? I predict a soon to follow connection not available situation.
    – 321X
    Aug 12, 2011 at 14:32
  • Code is fixed. I was missing a character. setTimout instead of setTimeout!
    – 321X
    Aug 12, 2011 at 14:35
  • 1
    It works great, i will think a bit more if im getting this code published to my site since its obvoiusly a big risk if i have many visitors.
    – Paparappa
    Aug 12, 2011 at 14:52
1

The simple way

$(function(){
        setInterval(function(){
            $('#target').load('page.php');
        }, 500)
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.