定时器三----js定时器

方法一:
    
    var t;
    
    //初始化定时器
    $(function(){
        init_fun_timer1();
        
    });
    
    
    function init_fun_timer1(){
        
        //可执行方法
        alert(1);
        
        t = setTimeOut(function(){
            init_fun_timer1()
        }, 1000);
        /*
            还有一种写法: t = setTimOut("init_fun_timer1()",1000); 但不规范。            
         */
    }
    
    #tips:
        注意:这种定时不规范,在不同浏览器的效果可能不同。且该方法是死循环,建议用方法二

方法二:

//-----------------------------------------------------------------------
#示例如下:

    <head>    
        <script type="text/javascript">
                var timer = window.setInterval("queryExprieCount()",2000);
                function queryExprieCount(){
                    $.ajax({
                                type: "POST",
                                async:true,
                                cache:false,
                                url: basePath + 'pc/queryOutTimeCount.do',
                                dataType : "json",
                                success:function(data){
                                    if(data != 0){
                                        $("#pcExprieControl").html("");
                                        $("#pcExprieControl").html("存在"+data+"个已过期xx,请及时处理!");
                                        $("#pcExprieControl").show();
                                    }else{
                                        $("pcExprieControl").hide();
                                    }
                                },
                                error : function(xhr, status, errMsg) {
                                    com.message("error","监控失败!");                    
                                }
                            });
                
                }
            </script>
    </head>
        
    <body>
        <div id="pcExprieControl" value="" style="color: red;font-size: 20px;">
        </div>
    
    </body>
        
//----------------------------------------------------------------    
var timer = window.setInterval("scrollup()", 50);//第一个是方法;第二个是间隔时间,单位ms
clearInterval(timer);//暂停该定时器

原文地址:https://www.cnblogs.com/nnn123/p/6839187.html