JavaScript - jQuery interval

后端 未结 5 1100
执笔经年
执笔经年 2020-12-10 04:20

I am using JavaScript with jQuery. I have the following script to alert hi every 30 seconds.

$(document).ready( function() {
    alert(\"hi\");
         


        
5条回答
  •  既然无缘
    2020-12-10 05:05

    Wrap your code in a function. Then, pass the function as a first argument to setInterval, and call the function:

    $(document).ready( function() {
        //Definition of the function (non-global, because of the previous line)
        function hi(){
            alert("hi");
        }
    
        //set an interval
        setInterval(hi, 30000);
    
        //Call the function
        hi();
    });
    

提交回复
热议问题