javascript计时器更改颜色

时间:2018-11-25 15:24:26

标签: javascript timer colors countdown

我有一个倒计时,每当天,小时和/或分钟改变时,已改变的数字应以红色闪烁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>

1 个答案:

答案 0 :(得分:0)

保存每个时间单位,以便您可以比较它们,然后,如果它们已更改,请向它们添加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>