0

I have this simple script that keeps teasing me:

var latest;

function populate(){
    $.get('lib/log.php?action=update&latest='+latest, function(data) {
        if(data.id!=latest){
            latest = data.id;
            $(data.news).hide().prependTo('#tabul').slideDown("slow");
        }
    },"json")
    setInterval(populate,5000);
   };

populate();

- Every time the request fires it retrieves a single line of html, but it keeps adding it to the previous response, so it goes 1 line, 2 lines, 4 lines, 8, 16.. and so on.. the browser (and bandwidth) goes crazy...

Why is that? What am I missing and how do I just get it to prepend that single line only.

Thanks in advance

Andreas

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
user825608
  • 11
  • 3

4 Answers4

0

You should be using setTimeout instead of setInterval.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • And I agree with Arnaud, this question is a great resource for your problem: http://stackoverflow.com/questions/729921/settimeout-or-setinterval – Blazemonger Sep 14 '11 at 13:55
0

SetInterval is fired even tho the AJAX response may not be finished after those 5 seconds, may I recommend you using setTimeout after you have slideDown your new content?

function populate(){
    $.get('lib/log.php?action=update&latest='+latest, function(data) {
        if(data.id!=latest){
            latest = data.id;
            $(data.news).hide().prependTo('#tabul').slideDown("slow");
        }

        setTimeout(populate,5000);
    },"json")
};

populate();
voigtan
  • 8,953
  • 2
  • 29
  • 30
0

Change your setInterval by the setTimeout function.

Check this stack overflow thread for understanding the difference between these two.

Community
  • 1
  • 1
Arnaud Leymet
  • 5,995
  • 4
  • 35
  • 51
0

Maybe I'm misreading something... but you have your Populate function adding another setInterval every time it runs. So it's going to run one more time per interval every time it is executed.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123