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

时间:2014-07-03 06:57:05

标签: 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.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>

2 个答案:

答案 0 :(得分:2)

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

setInterval(function () {

使用:

var myInterval = setInterval(function () {

然后,在:

之后
var seconds_left = (target_date - current_date) / 1000;

插入:

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

这会在计时器达到0秒时清除间隔,并确保代码正确显示0

那么,这将是你的代码:

var target_date = new Date("Jul 3, 2014").getTime();
var days, hours, minutes, seconds;
var countdown = document.getElementById("countdown");

var myInterval = setInterval(function () {
    var current_date = new Date().getTime();
    var seconds_left = (target_date - current_date) / 1000;

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

    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);

    countdown.innerHTML = days + "d, " + hours + "h, "
    + minutes + "m, " + seconds + "s";  
}, 1000);

答案 1 :(得分:-1)

在函数末尾添加一个简单的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);

这是 JSFiddle

相关问题