猿问

一个简单的JavaScript倒数计时器的代码?

一个简单的JavaScript倒数计时器的代码?

我想使用一个简单的倒数计时器,从函数运行到0开始的30秒开始。没有毫秒。怎么编码?



九州编程
浏览 716回答 3
3回答

当年话下

var&nbsp;count=30;var&nbsp;counter=setInterval(timer,&nbsp;1000);&nbsp;//1000&nbsp;will&nbsp;&nbsp;run&nbsp;it&nbsp;every&nbsp;1&nbsp;secondfunction&nbsp;timer(){ &nbsp;&nbsp;count=count-1; &nbsp;&nbsp;if&nbsp;(count&nbsp;<=&nbsp;0) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clearInterval(counter); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//counter&nbsp;ended,&nbsp;do&nbsp;something&nbsp;here &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return; &nbsp;&nbsp;} &nbsp;&nbsp;//Do&nbsp;code&nbsp;for&nbsp;showing&nbsp;the&nbsp;number&nbsp;of&nbsp;seconds&nbsp;here}要使计时器的代码出现在段落中(或页面上的任何地方),只需将行:<span&nbsp;id="timer"></span>你想让秒出现的地方。然后在您的timer()函数,因此它看起来如下:function&nbsp;timer(){ &nbsp;&nbsp;count=count-1; &nbsp;&nbsp;if&nbsp;(count&nbsp;<=&nbsp;0) &nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clearInterval(counter); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return; &nbsp;&nbsp;} &nbsp;document.getElementById("timer").innerHTML=count&nbsp;+&nbsp;"&nbsp;secs";&nbsp;//&nbsp;watch&nbsp;for&nbsp;spelling}
随时随地看视频慕课网APP
我要回答