简体   繁体   中英

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>

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM