开发者社区> 问答> 正文

JS倒计时 使用setInterval调用对象自身方法报 Maximum call stack si

使用JavaScript写的倒计时,使用setInterval调用对象自身方法报 Maximum call stack size exceeded问题 Javascript代码 收藏代码

<!doctype html>  
<html>  
    <head>  
        <meta charset='utf-8'/>  
        <title>Title</title>  
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>  
    </head>  
    <body>  
        <div id='m1'><span><b>0</b><b>0</b></span> <span><b>0</b><b>0</b></span> <span><b>0</b><b>0</b></span></div>  
        <div id='m2'><span><b>0</b><b>0</b></span> <span><b>0</b><b>0</b></span> <span><b>0</b><b>0</b></span></div>  
        <script>  
            function CounterDown(id,endtime){  
                this.id=id;  
                this.endtime=new Date(endtime).getTime();  
                this._countTime();  
            }  
            CounterDown.prototype={  
                _countTime:function(){  
                    var timmer;  
                    var now =new Date().getTime();   
                    now = now+1000;  
                    var teamTimeId = this.id,  
                        endtime  = this.endtime,  
                        lasttime = (parseInt(endtime)-parseInt(now)) / 1000,  
                        days     =  Math.floor(lasttime  / (24 * 3600)),  
                        lastSec  = (lasttime - days * 24 * 3600),  
                        hours    = Math.floor(lastSec/3600),  
                        minutes  = Math.floor((lastSec - hours * 3600)/60),  
                        seconds  = Math.floor(lastSec - (hours * 3600) - (minutes * 60));  
                      
                    if(lasttime <= 0){  
                        clearInterval(timmer);  
                        $('#'+teamTimeId).html("<span><b>0</b><b>0</b></span>天<span><b>0</b><b>0</b></span>时<span><b>0</b><b>0</b></span>分<span><b>0</b><b>0</b></span>秒");  
                    }else{  
                        day_0=parseInt(days/10);  
                        day_1=parseInt(days%10);  
                        hours_0=parseInt(hours/10);  
                        hours_1=parseInt(hours%10);  
                        minutes_0=parseInt(minutes/10);  
                        minutes_1=parseInt(minutes%10);  
                          
                        seconds_0=parseInt(seconds/10);  
                        seconds_1=parseInt(seconds%10);  
                          
                        var timerhtml = "";  
                        timerhtml += "<span><b>" + day_0+"</b><b>"+day_1 + "</b></span>天";  
                        timerhtml += "<span><b>" + hours_0+"</b><b>"+hours_1 + "</b></span>时";  
                        timerhtml += "<span><b>" + minutes_0+"</b><b>"+minutes_1 + "</b></span>分";  
                        timerhtml += "<span><b>" + seconds_0+"</b><b>"+seconds_1 + "</b></span>秒";  
                        $('#'+teamTimeId).html(timerhtml);  
                    }  
                    timmer=setInterval(this._countTime,1000);  
                }  
            };  
            new CounterDown('m1',"2014/01/01 10:10:10");      
        </script>  
    </body>  
</html>

在浏览器控制台报如下错误:

image.png

请JavaScript高手看看是怎么回事,要怎么解决才好,谢谢!

展开
收起
长安归故里. 2020-01-07 14:13:36 1004 0
1 条回答
写回答
取消 提交回答
  • <!doctype html>  
    <html>  
        <head>  
            <meta charset='utf-8'/>  
            <title>Title</title>  
            <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>  
        </head>  
        <body>  
            <div id='m1'><span><b>0</b><b>0</b></span> <span><b>0</b><b>0</b></span> <span><b>0</b><b>0</b></span></div>  
            <div id='m2'><span><b>0</b><b>0</b></span> <span><b>0</b><b>0</b></span> <span><b>0</b><b>0</b></span></div>  
            <script>  
                function CounterDown(id,endtime){  
                    this.id=id;  
                    this.endtime=new Date(endtime).getTime();  
                    this._countTime();  
                }  
                CounterDown.prototype={  
                    _countTime:function(){  
                        var timmer;  
                        var now =new Date().getTime();   
    //                  now = now+1000;  
                        var teamTimeId = this.id,  
                            endtime  = this.endtime,  
                            lasttime = (parseInt(endtime)-parseInt(now)) / 1000,  
                            days     =  Math.floor(lasttime  / (24 * 3600)),  
                            lastSec  = (lasttime - days * 24 * 3600),  
                            hours    = Math.floor(lastSec/3600),  
                            minutes  = Math.floor((lastSec - hours * 3600)/60),  
                            seconds  = Math.floor(lastSec - (hours * 3600) - (minutes * 60));  
                          
                        if(lasttime <= 0){  
                            clearTimeout(timmer);  
                            $('#'+teamTimeId).html("<span><b>0</b><b>0</b></span>天<span><b>0</b><b>0</b></span>时<span><b>0</b><b>0</b></span>分<span><b>0</b><b>0</b></span>秒");  
                        }else{  
                            day_0=parseInt(days/10);  
                            day_1=parseInt(days%10);  
                            hours_0=parseInt(hours/10);  
                            hours_1=parseInt(hours%10);  
                            minutes_0=parseInt(minutes/10);  
                            minutes_1=parseInt(minutes%10);  
                              
                            seconds_0=parseInt(seconds/10);  
                            seconds_1=parseInt(seconds%10);  
                              
                            var timerhtml = "";  
      
                            timerhtml += "<span><b>" + day_0+"</b><b>"+day_1 + "</b></span>天";  
                            timerhtml += "<span><b>" + hours_0+"</b><b>"+hours_1 + "</b></span>时";  
                            timerhtml += "<span><b>" + minutes_0+"</b><b>"+minutes_1 + "</b></span>分";  
                            timerhtml += "<span><b>" + seconds_0+"</b><b>"+seconds_1 + "</b></span>秒";  
                            $('#'+teamTimeId).html(timerhtml);  
                        }  
                        var that = this;  
                        var timmer = setTimeout(function() {  
                            that._countTime();  
                        }, 500);  
                    }  
                };  
                new CounterDown('m1',"2014/01/01 10:10:10");      
            </script>  
        </body>  
    </html>
    
    2020-01-07 14:14:01
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript面向对象的程序设计 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载