NamiW
NamiW

Reputation: 1692

a js timer cannot run correctly

I put these code in the HEAD part of a html,but cannot run correctly,
is there any problem?

obviously it's a timer but it doesn't run even once.
I didnot forget to link the jquery.

thanks a lot.

<script type="text/javascript">
$(function(){
    function something(){
        alert("something happened");
    }

    var timer=function(){
        something();
        setTimeout(timer,900);
    };

    timer;
}
);
</script>

Upvotes: 2

Views: 83

Answers (1)

Quentin
Quentin

Reputation: 943098

Your outer anonymous function is run onready, but it just defines two function and then has a useless timer expression, you have to actually call the function: timer().

If you want to do this properly, you should probably get rid of the timer function entirely, as you seem to be trying to simulate setInterval.

$(function(){
    function something(){
        alert("something happened");
    }

    setInterval(something, 900);
});

Upvotes: 7

Related Questions