javascript倒数计时器到期后删除小时,分钟,秒..

时间:2018-07-02 20:24:12

标签: javascript html css timer countdown

我对javascript非常陌生,我不知道如何删除倒计时的过期部分。例如,当倒数计数结束时,它将删除天数,然后将删除小时数,依此类推,直到达到“过期”

这是JS代码:

modelTestedSelectedHandler

HTML:

undefined

非常感谢您的帮助人员

2 个答案:

答案 0 :(得分:0)

您可以检查时间段是否为0,并且所有较大的时间段也都为0。

if (day == 0) {
    day.innerHTML = "";
}
if (day == 0 && hour == 0) {
    hour.innerHTML = "";
}
if (day == 0 && hour == 0 && min == 0) {
    min.innerHTML = "";
}
if (day == 0 && hour == 0 && min == 0 && sec == 0) {
    sec.innerHTML = "";
    n.innerHTML = "Expired";
}

答案 1 :(得分:0)

最后,我能够解决我的错误,这就是我的做法: 而不是{{request.GET.date}} GMT-7提供您的参数,效果很好!

<script>
    var deadline = new Date("{{ request.GET.date }} GMT-7").getTime();

    var x = setInterval(function () {

        var now = new Date().getTime();
        var t = deadline - now;
        var days = Math.floor(t / (1000 * 60 * 60 * 24));
        var hours = Math.floor((t % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((t % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((t % (1000 * 60)) / 1000);

        if (days == 0) {
            document.getElementById("day").innerHTML = '';
            document.getElementById("day-title").innerHTML = '';
        } else if (days ==1) {
            document.getElementById("day-title").innerHTML = 'Day';
            document.getElementById("day").innerHTML = days;
        } else {
            document.getElementById("day").innerHTML = days;
            document.getElementById("day-title").innerHTML = 'Days';
        }
        if (hours != 0) {
            document.getElementById("hour").innerHTML = hours;
        } else {
            document.getElementById("hour").innerHTML = '';
            document.getElementById("hour-title").innerHTML = '';
        }

        document.getElementById("minute").innerHTML = minutes;
        document.getElementById("second").innerHTML = seconds;
        if (t < 0) {
            clearInterval(x);
            document.getElementById("clock").innerHTML = '';
        }
    }, 1000);
</script>