Mik*_*ike 143 jquery counter timer setinterval clearinterval
我想使用一个简单的倒数计时器,从运行函数30秒开始,到0结束.没有毫秒.怎么编码?
Cli*_*ote 251
var count=30;
var counter=setInterval(timer, 1000); //1000 will run it every 1 second
function timer()
{
count=count-1;
if (count <= 0)
{
clearInterval(counter);
//counter ended, do something here
return;
}
//Do code for showing the number of seconds here
}
Run Code Online (Sandbox Code Playgroud)
要使计时器的代码出现在段落中(或页面上的任何其他位置),只需输入以下行:
<span id="timer"></span>
Run Code Online (Sandbox Code Playgroud)
你希望秒出现在哪里.然后在timer()函数中插入以下行,如下所示:
function timer()
{
count=count-1;
if (count <= 0)
{
clearInterval(counter);
return;
}
document.getElementById("timer").innerHTML=count + " secs"; // watch for spelling
}
Run Code Online (Sandbox Code Playgroud)
CMS*_*CMS 101
我不久前写了这个脚本:
用法:
var myCounter = new Countdown({
seconds:5, // number of seconds to count down
onUpdateStatus: function(sec){console.log(sec);}, // callback for each second
onCounterEnd: function(){ alert('counter ended!');} // final action
});
myCounter.start();
Run Code Online (Sandbox Code Playgroud)
function Countdown(options) {
var timer,
instance = this,
seconds = options.seconds || 10,
updateStatus = options.onUpdateStatus || function () {},
counterEnd = options.onCounterEnd || function () {};
function decrementCounter() {
updateStatus(seconds);
if (seconds === 0) {
counterEnd();
instance.stop();
}
seconds--;
}
this.start = function () {
clearInterval(timer);
timer = 0;
seconds = options.seconds;
timer = setInterval(decrementCounter, 1000);
};
this.stop = function () {
clearInterval(timer);
};
}
Run Code Online (Sandbox Code Playgroud)
Nie*_*sol 53
到目前为止,答案似乎依赖于即时运行的代码.如果您将计时器设置为1000毫秒,它实际上将大约为1008.
这是你应该怎么做的:
function timer(time,update,complete) {
var start = new Date().getTime();
var interval = setInterval(function() {
var now = time-(new Date().getTime()-start);
if( now <= 0) {
clearInterval(interval);
complete();
}
else update(Math.floor(now/1000));
},100); // the smaller this number, the more accurate the timer will be
}
Run Code Online (Sandbox Code Playgroud)
要使用,请致电:
timer(
5000, // milliseconds
function(timeleft) { // called every step to update the visible countdown
document.getElementById('timer').innerHTML = timeleft+" second(s)";
},
function() { // what to do after
alert("Timer complete!");
}
);
Run Code Online (Sandbox Code Playgroud)
Lay*_*son 21
如果有人需要一分钟和秒钟,这是另一个:
var mins = 10; //Set the number of minutes you need
var secs = mins * 60;
var currentSeconds = 0;
var currentMinutes = 0;
/*
* The following line has been commented out due to a suggestion left in the comments. The line below it has not been tested.
* setTimeout('Decrement()',1000);
*/
setTimeout(Decrement,1000);
function Decrement() {
currentMinutes = Math.floor(secs / 60);
currentSeconds = secs % 60;
if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
secs--;
document.getElementById("timerText").innerHTML = currentMinutes + ":" + currentSeconds; //Set the element id you need the time put into.
if(secs !== -1) setTimeout('Decrement()',1000);
}
Run Code Online (Sandbox Code Playgroud)