我想像
那样倒计时:10,:09,:08
,依此类推。请帮助初学者。
JS:
<script type="text/javascript">
var timeleft = 30;
var downloadTimer = setInterval(function(){
timeleft--;
document.getElementById("countdowntimer").textContent = ":" + timeleft;
if(timeleft <= 0){
clearInterval(downloadTimer);
window.location.replace("next_slide.html");
}
},1000);
</script>
答案 0 :(得分:0)
这是您可以继续的方式:
<script type="text/javascript">
var timeleft = 30;
var downloadTimer = setInterval(function(){
timeleft--;
document.getElementById("countdowntimer").textContent = ":" + ((timeleft >= 10) ? timeleft : ("0" + timeleft));
if(timeleft <= 0){
clearInterval(downloadTimer);
window.location.replace("next_slide.html");
}
},1000);
</script>
答案 1 :(得分:0)
我想这就是你要的:
var timeleft = 30;
var downloadTimer = setInterval(function(){
timeleft--;
if (timeleft < 10) {
suffix = ':0';
} else {
suffix = ':'
};
document.getElementById("countdowntimer").textContent = suffix + timeleft;
if(timeleft <= 0){
clearInterval(downloadTimer);
window.location.replace("next_slide.html");
}
},1000);
答案 2 :(得分:0)
我添加了一个工作计时器示例,以及通常的设置方法。我使用的是jQuery,但没有它,您会怎么做?
function startTimer() {
$("#timeIndicator").text(TIMER_CONSTANT);
//document.getElementById(“timeIndicator”).textContent = TIMER_CONSTANT;
setInterval(function() {
TIMER_CONSTANT --;
if(TIMER_CONSTANT >=0) {
$("#timeIndicator").text(TIMER_CONSTANT);
//document.getElementById(“timeIndicator”).textContent = TIMER_CONSTANT;
}
if(TIMER_CONSTANT === 0) {
clearInterval(TIMER_CONSTANT);
}
}, 1000);
}
您的countdownTimer
元素是什么?您的代码中目前正在发生什么?任何意外输出是什么?