【问题标题】:javascript timer changing colorjavascript定时器改变颜色
【发布时间】:2026-02-14 02:55:01
【问题描述】:

我有一个倒计时,每次天、小时和/或分钟改变时,改变的数字会闪烁红色 0.5 秒。

我不知道该怎么做...

这是计时器:

<script>
            var countDownDate = new Date("Jan 10, 2019 00:00:00").getTime();
            var x = setInterval(function() {

            //datum und zeit getten
            var now = new Date().getTime();

            // Find the distance between now and the count down date
            var distance = countDownDate - now;

            // Time calculations for days, hours, minutes and seconds
            var days = Math.floor(distance / (1000 * 60 * 60 * 24));
            var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = Math.floor((distance % (1000 * 60)) / 1000);


            // Display the result in the element with id="demo"
            document.getElementById("demo").innerHTML = "Noch "  + days + " Tage, " + hours + " Stunden, "
            + minutes + " Minuten und " + seconds + " Sekunden bis zur Fertigstellung des Spiels!!! ";
            if(days==0){
                document.getElementById("demo").innerHTML = "Nur noch "  + hours + " Stunden, "
            + minutes + " Minuten und " + seconds + " Sekunden bis zur Fertigstellung des Spiels!!! ";
            }
            if(days==0 && hours==0){
                document.getElementById("demo").innerHTML = "Nur noch " + minutes + " Minuten und " + seconds + " Sekunden bis zur Fertigstellung des Spiels!!! ";

            }
            if(days==0 && hours==0 && minutes==0){
                document.getElementById("demo").innerHTML= "Nur noch" + seconds + " Sekunden bis zur Fertigstellung des Spiels!!! ";

            } 
            // If the count down is finished, write some text 
            if (distance < 0) {
                clearInterval(x);
                document.getElementById("demo").innerHTML = "VERÖFFENTLICHT!";
            } 
            if(days % 3==0){
                document.getElementById("sup").style.display ="block";   
                  }else {
                    document.getElementById("sup").style.display ="none"; 
            }, 1000);

     </script>

【问题讨论】:

  • 欢迎来到 Stack Overflow。请始终包含所有相关代码(HTML、CSS 和 JavaScript),以便我们可以复制您的问题并为您提供有效的答案。但是,也请注意,我们不是代码编写服务或教程网站。我们希望您自己进行研究并尝试解决问题。然后,如果您遇到特定问题,您可以在此处提出相关问题。就目前而言,您实际上只是在寻求完整的解决方案,而没有先进行任何尝试。

标签: javascript timer colors countdown


【解决方案1】:

保存每个时间单位,以便您可以比较它们,如果它们发生了变化,请将 class="active" 添加到它们。 500 毫秒后移除活动类。

<style>
    .active {
        color: red;
    }
</style>
<div id="demo"></div>
<div id="sup"></div>
    <script>
        var countDownDate = new Date("Jan 10, 2019 00:00:00").getTime();

        var old_days = 0;
        var old_hours = 0;
        var old_minutes = 0;
        var old_seconds = 0;

        function updateActiveTimer(unit, number) {
            switch(unit) {
                case 'days':
                    if(number==old_days) {
                        return number;
                    }
                    old_days = number;
                    break;
                case 'hours':
                    if(number==old_hours) {
                        return number;
                    }
                    old_hours = number;
                    break;
                case 'minutes':
                    if(number==old_minutes) {
                        return number;
                    }
                    old_minutes = number;
                    break;
                case 'seconds':
                    if(number==old_seconds) {
                        return number;
                    }
                    old_seconds = number;
                    break;
            }

            setTimeout(function() {
                resetActiveTimer();
            }, 500);

            return '<span class="active">'+number+'</span>';
        }

        function resetActiveTimer() {
            var activeElems = document.getElementsByClassName('active');
            for(var i=0;i<activeElems.length;i++) {
                activeElems[i].classList.remove('active');
            }
        }

        var x = setInterval(function() {

            //datum und zeit getten
            var now = new Date().getTime();

            // Find the distance between now and the count down date
            var distance = countDownDate - now;

            // Time calculations for days, hours, minutes and seconds
            var days = Math.floor(distance / (1000 * 60 * 60 * 24));
            var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
            var seconds = Math.floor((distance % (1000 * 60)) / 1000);


            // Display the result in the element with id="demo"
            document.getElementById("demo").innerHTML = "Noch "  + updateActiveTimer('days', days) + " Tage, " + updateActiveTimer('hours', hours) + " Stunden, " + updateActiveTimer('minutes', minutes) + " Minuten und " + updateActiveTimer('seconds', seconds) + " Sekunden bis zur Fertigstellung des Spiels!!! ";
            if(days==0){
                document.getElementById("demo").innerHTML = "Nur noch "  + updateActiveTimer('hours', hours) + " Stunden, " + updateActiveTimer('minutes', minutes) + " Minuten und " + updateActiveTimer('seconds', seconds) + " Sekunden bis zur Fertigstellung des Spiels!!! ";
            }
            if(days==0 && hours==0){
                document.getElementById("demo").innerHTML = "Nur noch " + updateActiveTimer('minutes', minutes) + " Minuten und " + updateActiveTimer('seconds', seconds) + " Sekunden bis zur Fertigstellung des Spiels!!! ";

            }
            if(days==0 && hours==0 && minutes==0){
                document.getElementById("demo").innerHTML= "Nur noch" + updateActiveTimer('seconds', seconds) + " Sekunden bis zur Fertigstellung des Spiels!!! ";
            } 
            // If the count down is finished, write some text 
            if (distance < 0) {
                clearInterval(x);
                document.getElementById("demo").innerHTML = "VERÖFFENTLICHT!";
            } 
            if(days % 3==0){
                document.getElementById("sup").style.display ="block";   
            }else {
                document.getElementById("sup").style.display ="none";
            }
        }, 1000);

    </script>

【讨论】:

  • 工作得很好,但我没有注意到任务是让天的小时和分钟闪烁。秒应该保持白色