JS倒计时 使用setInterval调用对象自身方法报 Maximum call stack size exceeded问题

使用JavaScript写的倒计时,使用setInterval调用对象自身方法报 Maximum call stack size exceeded问题
[code="javaScript"]<!doctype html>



Title



00 00 00

00 00 00

<br> function CounterDown(id,endtime){<br> this.id=id;<br> this.endtime=new Date(endtime).getTime();<br> this._countTime();<br> }<br> CounterDown.prototype={<br> _countTime:function(){<br> var timmer;<br> var now =new Date().getTime(); <br> now = now+1000;<br> var teamTimeId = this.id,<br> endtime = this.endtime,<br> lasttime = (parseInt(endtime)-parseInt(now)) / 1000,<br> days = Math.floor(lasttime / (24 * 3600)),<br> lastSec = (lasttime - days * 24 * 3600),<br> hours = Math.floor(lastSec/3600),<br> minutes = Math.floor((lastSec - hours * 3600)/60),<br> seconds = Math.floor(lastSec - (hours * 3600) - (minutes * 60));</p> <pre><code> if(lasttime &lt;= 0){ clearInterval(timmer); $(&#39;#&#39;+teamTimeId).html(&quot;&lt;span&gt;&lt;b&gt;0&lt;/b&gt;&lt;b&gt;0&lt;/b&gt;&lt;/span&gt;天&lt;span&gt;&lt;b&gt;0&lt;/b&gt;&lt;b&gt;0&lt;/b&gt;&lt;/span&gt;时&lt;span&gt;&lt;b&gt;0&lt;/b&gt;&lt;b&gt;0&lt;/b&gt;&lt;/span&gt;分&lt;span&gt;&lt;b&gt;0&lt;/b&gt;&lt;b&gt;0&lt;/b&gt;&lt;/span&gt;秒&quot;); }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 = &quot;&quot;; timerhtml += &quot;&lt;span&gt;&lt;b&gt;&quot; + day_0+&quot;&lt;/b&gt;&lt;b&gt;&quot;+day_1 + &quot;&lt;/b&gt;&lt;/span&gt;天&quot;; timerhtml += &quot;&lt;span&gt;&lt;b&gt;&quot; + hours_0+&quot;&lt;/b&gt;&lt;b&gt;&quot;+hours_1 + &quot;&lt;/b&gt;&lt;/span&gt;时&quot;; timerhtml += &quot;&lt;span&gt;&lt;b&gt;&quot; + minutes_0+&quot;&lt;/b&gt;&lt;b&gt;&quot;+minutes_1 + &quot;&lt;/b&gt;&lt;/span&gt;分&quot;; timerhtml += &quot;&lt;span&gt;&lt;b&gt;&quot; + seconds_0+&quot;&lt;/b&gt;&lt;b&gt;&quot;+seconds_1 + &quot;&lt;/b&gt;&lt;/span&gt;秒&quot;; $(&#39;#&#39;+teamTimeId).html(timerhtml); } timmer=setInterval(this._countTime,1000); } }; new CounterDown(&#39;m1&#39;,&quot;2014/01/01 10:10:10&quot;); &lt;/script&gt; &lt;/body&gt; </code></pre> <p></html>[/code]<br> 在浏览器控制台报如下错误:</p> <p>[img]<a href="http://dl2.iteye.com/upload/attachment/0090/5946/f09d22c5-f35a-3685-8338-5caccad91105.png%5B/img%5D">http://dl2.iteye.com/upload/attachment/0090/5946/f09d22c5-f35a-3685-8338-5caccad91105.png[/img]</a></p> <p>请JavaScript高手看看是怎么回事,要怎么解决才好,谢谢!</p>

2个回答

[code="java"]
<!doctype html>



Title



00 00 00

00 00 00

<br> function CounterDown(id,endtime){<br> this.id=id;<br> this.endtime=new Date(endtime).getTime();<br> this._countTime();<br> }<br> CounterDown.prototype={<br> _countTime:function(){<br> var timmer;<br> var now =new Date().getTime(); <br> // now = now+1000;<br> var teamTimeId = this.id,<br> endtime = this.endtime,<br> lasttime = (parseInt(endtime)-parseInt(now)) / 1000,<br> days = Math.floor(lasttime / (24 * 3600)),<br> lastSec = (lasttime - days * 24 * 3600),<br> hours = Math.floor(lastSec/3600),<br> minutes = Math.floor((lastSec - hours * 3600)/60),<br> seconds = Math.floor(lastSec - (hours * 3600) - (minutes * 60));</p> <pre><code> if(lasttime &lt;= 0){ clearTimeout(timmer); $(&#39;#&#39;+teamTimeId).html(&quot;&lt;span&gt;&lt;b&gt;0&lt;/b&gt;&lt;b&gt;0&lt;/b&gt;&lt;/span&gt;天&lt;span&gt;&lt;b&gt;0&lt;/b&gt;&lt;b&gt;0&lt;/b&gt;&lt;/span&gt;时&lt;span&gt;&lt;b&gt;0&lt;/b&gt;&lt;b&gt;0&lt;/b&gt;&lt;/span&gt;分&lt;span&gt;&lt;b&gt;0&lt;/b&gt;&lt;b&gt;0&lt;/b&gt;&lt;/span&gt;秒&quot;); }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 = &quot;&quot;; timerhtml += &quot;&lt;span&gt;&lt;b&gt;&quot; + day_0+&quot;&lt;/b&gt;&lt;b&gt;&quot;+day_1 + &quot;&lt;/b&gt;&lt;/span&gt;天&quot;; timerhtml += &quot;&lt;span&gt;&lt;b&gt;&quot; + hours_0+&quot;&lt;/b&gt;&lt;b&gt;&quot;+hours_1 + &quot;&lt;/b&gt;&lt;/span&gt;时&quot;; timerhtml += &quot;&lt;span&gt;&lt;b&gt;&quot; + minutes_0+&quot;&lt;/b&gt;&lt;b&gt;&quot;+minutes_1 + &quot;&lt;/b&gt;&lt;/span&gt;分&quot;; timerhtml += &quot;&lt;span&gt;&lt;b&gt;&quot; + seconds_0+&quot;&lt;/b&gt;&lt;b&gt;&quot;+seconds_1 + &quot;&lt;/b&gt;&lt;/span&gt;秒&quot;; $(&#39;#&#39;+teamTimeId).html(timerhtml); } var that = this; var timmer = setTimeout(function() { that._countTime(); }, 500); } }; new CounterDown(&#39;m1&#39;,&quot;2014/01/01 10:10:10&quot;); &lt;/script&gt; &lt;/body&gt; </code></pre> <p></html><br> [/code]</p>
yangmingge
yangmingge 谢谢了哈!
接近 7 年之前 回复

死循环了 最后一个地使用如下代码解决死循环
setTimeout(function() {
timmer=setInterval(this._countTime,1000);

}, 0)

yangmingge
yangmingge 改了试了,还是不行
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问