想要停止倒计时并提供输出时间

时间:2012-09-26 14:18:10

标签: php javascript

你好,我想在我的倒计时结束时它应该给出:

document.getElementById("remain").innerHTML = "Time up";

现场倒计时:http://ffsng.deewayz.in/counter/

即将结束我想放弃“时间到了”

我尝试了一些这样的方法,但努力工作。

function setCountDown() {
    seconds--;
    if (seconds < 0) {
        minutes--;
        seconds = 59;
    }
    if (minutes < 0) {
        hours--;
        minutes = 59;
    }
    if (hours < 0) {
        days--;
        hours = 23;
    }
    document.getElementById("remain").innerHTML = days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";
    if ((seconds = 0) && (minutes = 0) && (hours = 0) && (days = 0))(
        document.getElementById("remain").innerHTML = "time up";
    ) else ( 
        setTimeout("setCountDown()", 1000);
    );  
}

我的完整代码在这里:Countdown timer not working with gmt time

2 个答案:

答案 0 :(得分:3)

不太好,但是你的需求还可以!

<强> HTML

<div id="countdown">
    <span id="row1"></span><br />
    <span id="row2"></span>
</div>

<强> JS

var end = new Date('October 08, 2012 00:55:00');

function toSt1(n) {
    var s = '';
    if (n < 10) s += '0';
    return (s + n).toString();
}

function toSt2(n) {
    var s = '';
    if (n < 10) s += '00';
    else if (n < 100) s += '0';
    return (s + n).toString();
}

var countdown = function() {
    var d = new Date();
    var count = Math.floor(end.getTime() - d.getTime());
    if (count > 0) {
        var miliseconds = toSt2(count % 1000);
        count = Math.floor(count / 1000);
        var seconds = toSt1(count % 60);
        count = Math.floor(count / 60);
        var minutes = toSt1(count % 60);
        count = Math.floor(count / 60);
        var hours = toSt1(count % 24);
        count = Math.floor(count / 24);
        var days = count;
        document.getElementById('row1').innerHTML = days + ' Days';
        document.getElementById('row2').innerHTML = hours + ' Hours ' + minutes + ' Minutes ' + seconds + ' Seconds ';
        setTimeout('countdown()', 100);
    } else {
        document.getElementById("countdown").innerHTML = "Time up";
    }
}
countdown();

<强> DEMO

答案 1 :(得分:1)

在完成倒计时时,请参阅此代码以执行某些操作(按照您的要求提供输出):

$(function(){
  var count = 10;
  countdown = setInterval(function(){
    $("p.countdown").html(count + " seconds remaining!");
    if (count == 0) {
      $('#remain').html('Time up');
    }
    count--;
  }, 1000);
});

此解决方案使用jQuery。

参考。 How can I make a jQuery countdown