倒计时到文件时,用文字替换倒计时?

时间:2017-12-29 15:15:53

标签: javascript

我试图在结束后用一些文字替换我的倒计时。但是,我似乎无法做到这一点。

以下是JavaScript代码:

function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function() {
        minutes = parseInt(timer / 60, 10)
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + " " + " " + seconds;

        if (--timer < 0) {
            timer = duration;
        }
        console.log(parseInt(seconds))
        window.localStorage.setItem("seconds", seconds)
        window.localStorage.setItem("minutes", minutes)
    }, 1000);
}

window.onload = function() {
    sec = parseInt(window.localStorage.getItem("seconds"))
    min = parseInt(window.localStorage.getItem("minutes"))

    if (parseInt(min * sec)) {
        var fiveMinutes = (parseInt(min * 60) + sec);
    } else {
        var fiveMinutes = 60 * 5;
    }
    // var fiveMinutes = 60 * 5;
    display = document.querySelector('#time');
    startTimer(fiveMinutes, display);
};

2 个答案:

答案 0 :(得分:0)

试试这个:

if (--timer < 0) {
    display.textContent = "some text";
}

答案 1 :(得分:0)

这样的事情:

if (--timer < 0) {
    timer = duration;
    display.textContent = minutes + " " + " " + seconds;
}else
    display.textContent = "some text";