两位数的 30 秒倒计时

Double digit 30 seconds countdown

我想要像

这样的倒计时

: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>

这是您可以继续的方式:

<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>

我想这就是你想要的:

    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);

我添加了一个工作计时器示例以及我通常如何设置它。我正在使用 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 元素是什么?您的代码中当前发生了什么? any 的意外输出是什么?