Javascript 计时器结束时,如何在00:00:00:00冻结倒计时

Javascript 计时器结束时,如何在00:00:00:00冻结倒计时,javascript,jquery,timer,Javascript,Jquery,Timer,我想把计时器冻结在00:00:00:00,当计时器结束时怎么做? 非常感谢 塔克斯 这是我的密码 // set the date we're counting down to var target_date = new Date("Jul 3, 2014").getTime(); // variables for time units var days, hours, minutes, seconds; // get tag element var countdown = document.

我想把计时器冻结在00:00:00:00,当计时器结束时怎么做? 非常感谢

塔克斯

这是我的密码

// set the date we're counting down to
var target_date = new Date("Jul 3, 2014").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
setInterval(function () {

    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;

    // do some time calculations
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;

    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;

    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);

    // format countdown string + set tag value
    countdown.innerHTML = days + "d, " + hours + "h, "
    + minutes + "m, " + seconds + "s";  

}, 1000);
HTML

<span id="countdown"></span>

由于不再需要间隔,您可以更换:

setInterval(function () {
与:

然后,在:

var seconds_left = (target_date - current_date) / 1000;
插入:

if(seconds_left <= 0){
    seconds_left = 0;
    clearInterval(myInterval);
}

在函数末尾添加一个简单的IF:

    // format countdown string + set tag value
    if(days > 0 || hours > 0 || minutes > 0 || seconds > 0)
        countdown.innerHTML = days + "d, " + hours + "h, "
    + minutes + "m, " + seconds + "s"; 

    else
       countdown.innerHTML = 0 + "d, " + 0 + "h, "
    + 0 + "m, " + 0 + "s";  
在将实际时间写入div之前,必须首先检查一个或多个值是否大于0

因此,您的代码如下所示:

// set the date we're counting down to
var target_date = new Date("Jul 3, 2014").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
setInterval(function () {

    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;

    // do some time calculations
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;

    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;

    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);

    // format countdown string + set tag value
    if(days > 0 || hours > 0 || minutes > 0 || seconds > 0)
        countdown.innerHTML = days + "d, " + hours + "h, "
    + minutes + "m, " + seconds + "s"; 

    else
       countdown.innerHTML = 0 + "d, " + 0 + "h, "
    + 0 + "m, " + 0 + "s";  

}, 1000);

这里是工作

和所有其他字段?是这样吗?与小时、天、分钟类似,您可以从
seconds\u left
构建剩余的天/小时/分钟数。只有当
seconds\u left
0
时,所有其他变量将为
0
。因此,您只需检查剩下的
秒数
。此答案不完整且效率低下@初学者先生,你为什么接受这个答案?但是你的答案也不完全有效,但是我接受这个答案,但我没有测试它。我的答案确实有效,@Mr.初学者。这个答案的问题是,当剩余时间为0或更少时,间隔会一直运行。我的回答既有效又简短。
    // format countdown string + set tag value
    if(days > 0 || hours > 0 || minutes > 0 || seconds > 0)
        countdown.innerHTML = days + "d, " + hours + "h, "
    + minutes + "m, " + seconds + "s"; 

    else
       countdown.innerHTML = 0 + "d, " + 0 + "h, "
    + 0 + "m, " + 0 + "s";  
// set the date we're counting down to
var target_date = new Date("Jul 3, 2014").getTime();

// variables for time units
var days, hours, minutes, seconds;

// get tag element
var countdown = document.getElementById("countdown");

// update the tag with id "countdown" every 1 second
setInterval(function () {

    // find the amount of "seconds" between now and target
    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;

    // do some time calculations
    days = parseInt(seconds_left / 86400);
    seconds_left = seconds_left % 86400;

    hours = parseInt(seconds_left / 3600);
    seconds_left = seconds_left % 3600;

    minutes = parseInt(seconds_left / 60);
    seconds = parseInt(seconds_left % 60);

    // format countdown string + set tag value
    if(days > 0 || hours > 0 || minutes > 0 || seconds > 0)
        countdown.innerHTML = days + "d, " + hours + "h, "
    + minutes + "m, " + seconds + "s"; 

    else
       countdown.innerHTML = 0 + "d, " + 0 + "h, "
    + 0 + "m, " + 0 + "s";  

}, 1000);