时间开始在减去读数,而结束消息仍然显示

时间:2017-04-01 06:17:17

标签: javascript countdown

我想在网站上放置的倒数计时器有一些问题。倒计时是正常的,一切都很好,但在计时器结束时,它不是清除计时器并显示结束消息或回叫,而是在时间继续读取的同时显示结束消息。负

有谁能告诉我出了什么问题?

这是我的代码:

 
    // Set the date we're counting down to
    var countDownDate = new Date("March 31, 2017 09:35:00 PM").getTime();
    // Update the count down every 1 second
    
    var x = setInterval(function() {
    
        // Get todays date and time
        var now = new Date().getTime();
             
        // Find the distance between now an 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);
        
             
       
       // Output the result in an element with id="demo"
      
        document.getElementById("days").innerHTML = days; 
        document.getElementById("hours").innerHTML = hours;
        document.getElementById("minutes").innerHTML = minutes;
        document.getElementById("seconds").innerHTML = seconds;
    	
    	
    	 
        
        // If the count down is over, write some text 
    	if (distance < 0) {
            clearInterval(x);
            document.getElementById("endmessage").innerHTML = "EXPIRED";
    		
        }
       
    }, 1000);
    
body {
    			background: #f6f6f6;
    		}
    
    		.countdownContainer{
    			position: absolute;;
    			top: 50%;
    			left: 50%;
    			transform : translateX(-50%) translateY(-50%);
    			text-align: center;
    			background: #ddd;
    			border: 1px solid #999;
    			padding: 10px;
    			box-shadow: 0 0 5px 3px #ccc;
    		}
    
    		.info {
    			font-size: 80px;
    		}
<!DOCTYPE HTML>
    <html>
    <head>

    </head>
    <body>
    <table class="countdownContainer">
    			<tr class="info">
    				<td colspan="4">Countdown to April fool</td>
    			</tr>
    			<tr class="info">
    				<td id="days">00</td>
    				<td id="hours">00</td>
    				<td id="minutes">00</td>
    				<td id="seconds">00</td>
    				
    				<td id="endmessage"></td>
    			</tr>
    			<tr>
    				<td>Days</td>
    				<td>Hours</td>
    				<td>Minutes</td>
    				<td>Seconds</td>
    			</tr>
    		</table>
    		
    <p id="demo"></p>
    
    <script>
     
    </script>
    
    </body>
    
       
    </html>

2 个答案:

答案 0 :(得分:0)

它不是4月1日吗?它正在倒计时到3月31日已经过去了一天?

此外,您不需要PM,删除PM,只有09或21等不是AM / PM

<script>

// Set the date we're counting down to
var countDownDate = new Date("April 1, 2017 09:35:00").getTime();
// Update the count down every 1 second

var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();

    // Find the distance between now an 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);



   // Output the result in an element with id="demo"

    document.getElementById("days").innerHTML = days; 
    document.getElementById("hours").innerHTML = hours;
    document.getElementById("minutes").innerHTML = minutes;
    document.getElementById("seconds").innerHTML = seconds;




    // If the count down is over, write some text 
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("endmessage").innerHTML = "EXPIRED";

    }

}, 1000);

</script>

今天倒计时到09:35。

答案 1 :(得分:0)

我对您的代码进行了一些更改。请在下面找到,

&#13;
&#13;
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
        body {
            background: #f6f6f6;
        }

        .countdownContainer{
            position: absolute;;
            top: 50%;
            left: 50%;
            transform : translateX(-50%) translateY(-50%);
            text-align: center;
            background: #ddd;
            border: 1px solid #999;
            padding: 10px;
            box-shadow: 0 0 5px 3px #ccc;
        }

        .info {
            font-size: 80px;
        }
        </style>
</head>
<body>
<table class="countdownContainer">
            <tr class="info">
                <td colspan="4">Countdown to April fool</td>
            </tr>
            <tr class="info">
                <td id="days">00</td>
                <td id="hours">00</td>
                <td id="minutes">00</td>
                <td id="seconds">00</td>

                <td id="endmessage"></td>
            </tr>
            <tr>
                <td>Days</td>
                <td>Hours</td>
                <td>Minutes</td>
                <td>Seconds</td>
            </tr>
        </table>

<p id="demo"></p>

<script>

// Set the date we're counting down to
var countDownDate = new Date("April 01, 2017 12:00:30 PM").getTime();
// Update the count down every 1 second

var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();

    // Find the distance between now an 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);







    // If the count down is over, write some text 
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("endmessage").innerHTML = "EXPIRED";

    }
    else{
   // Output the result in an element with id="demo"

    document.getElementById("days").innerHTML = days; 
    document.getElementById("hours").innerHTML = hours;
    document.getElementById("minutes").innerHTML = minutes;
    document.getElementById("seconds").innerHTML = seconds;
    }

}, 1000);

</script>

</body>


</html>
&#13;
&#13;
&#13;

每次打印倒计时时,只有当差异大于0时才应该进行倒计时。因此,将该部分移动到IF的else条件中。相应地调整日期以测试您的倒计时。