好吧,我发现这个代码在 这个帖子 而我想在分和秒的基础上增加小时,谁能帮帮我--我对jquery不是很在行 :(
下面是代码。
<div id=timer></div>
<script type="text/javascript">
var timeoutHandle;
function countdown(minutes, seconds) {
function tick() {
var counter = document.getElementById("timer");
counter.innerHTML =
minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
seconds--;
if (seconds >= 0) {
timeoutHandle = setTimeout(tick, 1000);
} else {
if (minutes >= 1) {
// countdown(mins-1); never reach “00″ issue solved:Contributed by Victor Streithorst
setTimeout(function () {
countdown(minutes - 1, 59);
}, 1000);
}
}
}
tick();
}
countdown(1, 30);
</script>
所以,为了清楚起见,我需要传递3个变量。 countdown(hour, mins, secs);
- 非常感谢!
所以,经过一段时间的玩弄这段代码,我想我已经明白了.我不知道它是否是最好的方法,但从我的测试来看,它似乎可以工作到24小时。
这里是代码,如果你可以添加到它或改进它,请做,因为我不是很好的java脚本:(
<div id=timer></div>
<script type="text/javascript">
var timeoutHandle;
function countdown(hours, minutes, seconds) {
function tick() {
var counter = document.getElementById("timer");
counter.innerHTML = hours.toString() + ":" + (minutes < 10 ? "0" : "") + String(minutes) + ":" + (seconds < 10 ? "0" : "") + String(seconds);
seconds--;
if (seconds >= 0) {
timeoutHandle = setTimeout(tick, 1000);
} else {
if (minutes >= 1) {
setTimeout(function () { countdown(hours, minutes - 1, 59); }, 1000);
}
else if (hours >= 1) {
setTimeout(function () { countdown(hours - 1, 59, 59); }, 1000);
}
}
}
tick();
}
countdown(2,30,10);
</script>
我希望有人发现这个有用和有帮助:)