63

How do I get this javascript to run every second?

source code:

<script type="text/javascript">
$(function() {
    //More Button
    $('.more').live("click",function()  {
        var ID = $(this).attr("id");
        if(ID) {
            $("#more"+ID).html('<img src="moreajax.gif" />');

            $.ajax({
                type: "POST",
                url: "ajax_more.php",
                data: "lastmsg="+ ID, 
                cache: false,
                success: function(html){
                    $("ol#updates").prepend(html);
                    $("#more"+ID).remove();
                }
            });
        } else {
            $(".morebox").html('no posts to display');
        }

        return false;

    });
});

</script>
peterh
  • 11,875
  • 18
  • 85
  • 108
user663049
  • 1,160
  • 3
  • 14
  • 28

6 Answers6

111

Use setInterval() to run a piece of code every x milliseconds.

You can wrap the code you want to run every second in a function called runFunction.

So it would be:

var t=setInterval(runFunction,1000);

And to stop it, you can run:

clearInterval(t);
Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
12

Use setInterval:

$(function(){
setInterval(oneSecondFunction, 1000);
});

function oneSecondFunction() {
// stuff you want to do every second
}

Here's an article on the difference between setTimeout and setInterval. Both will provide the functionality you need, they just require different implementations.

Town
  • 14,706
  • 3
  • 48
  • 72
  • 1
    I'm getting an error message when I tried (multiple times) to edit: "Suggested edit queue is full" (I've never seen that message before). The `about.com` link is dead. About.com was rebranded to `Dotdash.com` which split up subdomains and categories from About.com to multiple domains like `thoughtco.com` and many other verticals. According to wired.com, this happened on May 2 2017. Here is an archive copy (on Archive.org from 2016): [setTimeout or setInterval](http://web.archive.org/web/20160924165849/http://javascript.about.com/library/blstvsi.htm). – Kevin Fegan Feb 26 '21 at 06:47
  • Thanks @KevinFegan - not sure why you can't edit, but I've updated the link now. Good spot :) – Town Feb 26 '21 at 10:41
6

You can use setTimeout to run the function/command once or setInterval to run the function/command at specified intervals.

var a = setTimeout("alert('run just one time')",500);
var b = setInterval("alert('run each 3 seconds')",3000);

//To abort the interval you can use this:
clearInterval(b);
Vismari
  • 745
  • 3
  • 12
1
window.setTimeout(func,1000);

This will run func after 1000 milliseconds. So at the end of func you can call window.setTimeout again to go in a loop of 1 sec. You just need to define a terminate condition.

Reference

Nitin Midha
  • 2,258
  • 20
  • 22
1

You can use setInterval:

var timer = setInterval( myFunction, 1000);

Just declare your function as myFunction or some other name, and then don't bind it to $('.more')'s live event.

Christopher Armstrong
  • 7,907
  • 2
  • 26
  • 28
0

Use setInterval(func, delay) to run the func every delay milliseconds.

setTimeout() runs your function once after delay milliseconds -- it does not run it repeatedly. A common strategy is to run your code with setTimeout and call setTimeout again at the end of your code.

Stephen P
  • 14,422
  • 2
  • 43
  • 67