使用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>
在浏览器控制台报如下错误:
请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){
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>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。