Ben Rogmans
Ben Rogmans

Reputation: 986

jQuery Timer plugin only runs once instead of each X seconds

I'm using jQuery and the jQuery Timers plugin: http://plugins.jquery.com/files/jquery.timers-1.2.js.txt

What I want to do is post data to a php script every 3 seconds. The script checks if there are new messages and if there are it will return them. I'm having a problem with the timer. Right now, it only runs the function once, after three seconds. After it has run one time, it doesn't run anymore.

$(function() {
$('.check').everyTime(3000, function() {

var ID = $(this).attr("id");
    if(ID) {
        $("#check"+ID).html('<img src="img/loader.gif" />');
    $.ajax({
        type: "POST",
        url: "<?php echo $base;?>ajax_check_new.php",
        data: "latestmsg="+ ID, 
        cache: false,

        success: function(html){
            $("ol#msg").prepend(html);
            $("#check"+ID).remove();
        }
    });
}

  });
});

Any suggestions?

Thanks in advance!!

Upvotes: 0

Views: 462

Answers (1)

mattsven
mattsven

Reputation: 23293

Is there a reason you are using jQuery Timers for this instead of setInterval?

EDIT:

$(function() {
    setInterval(function() {
        var ID = $('.check').attr("id");
            if(ID) {
                $("#check"+ID).html('<img src="img/loader.gif" />');
                $.ajax({
                    type: "POST",
                    url: "<?php echo $base;?>ajax_check_new.php",
                    data: "latestmsg="+ ID, 
                    cache: false,

                    success: function(html){
                        $("ol#msg").prepend(html);
                        $("#check"+ID).remove();
                    }
                });
            }
    }, 3000);
});

Upvotes: 1

Related Questions