倒数计时器JS

时间:2014-10-17 22:58:14

标签: countdowntimer

大家好我有一个javascript倒计时器,没有插入一天。好吧,我想为它添加当天报告的正确公式。这是我的js代码:(对不起我的英文,我使用谷歌翻译)

var tick = function(init) {
    if (init == undefined) {
        init = false;
    }
    var current_time = new Date();
    current_time = current_time.valueOf();
    if (current_time > target_time) {
        var time = new Date();
        target_time = new Date(time.getFullYear(), time.getMonth(), time.getDate());
        target_time = target_time.valueOf()+1000*60*60*24*45;           
    }

    var time_diff = Math.floor((target_time - current_time)/1000);
    //alert(time_diff); 
    var second_2 = time_diff % 10; time_diff = Math.floor(time_diff/10);
    var second_1 = time_diff % 6; time_diff = Math.floor(time_diff/6);
    var minute_2 = time_diff % 10; time_diff = Math.floor(time_diff/10);
    var minute_1 = time_diff % 6; time_diff = Math.floor(time_diff/6);
    var hour_2 = time_diff % 10; time_diff = Math.floor(time_diff/10);
    var hour_1 = time_diff % 6; time_diff = Math.floor(time_diff/6);

    var day_2 = Math.floor(time_diff/24);
    var day_1 = time_diff % 24;

    set_slide('span.secondPlay', second_2);

    if ((second_2 == 9) || init) {set_slide('span.second6Play', second_1);
    if ((second_1 == 5) || init) {set_slide('span.minutePlay', minute_2);
    if ((minute_2 == 9) || init) {set_slide('span.minute6Play', minute_1);
    if ((minute_1 == 5) || init) {set_slide('span.hourPlay', hour_2);
    if ((hour_2 == 9) || init) {set_slide('span.hour2Play', hour_1);

    if ((hour_1 == 1) || init) {set_slide('span.dayPlay', day_2);
    if ((day_2 == 9) || init) {set_slide('span.day1Play', day_1);}}}}}}}
  }
tick(true);
setInterval(tick, 1000);

这是我的HTML`

                            <div class="timer">
                            <div class="container" style = 'padding-top:0px;'>
                                <div class="days">
                                    <span class="flip day1Play"></span>
                                    <span class="flip dayPlay"></span>
                                    <em>дней</em>
                                </div>
                                <div class="hours">
                                    <span class="flip hour2Play"></span>
                                    <span class="flip hourPlay"></span>
                                    <em>часов</em>
                                </div>
                                <div class="minutes">
                                    <span class="flip minute6Play"></span>
                                    <span class="flip minutePlay"></span>
                                    <em>минут</em>
                                </div>
                                <div class="seconds">
                                    <span class="flip second6Play"></span>
                                    <span class="flip secondPlay"></span>
                                    <em>секунд</em>
                                </div>
                            </div>
                        </div>

我的问题是我无法写出正确的公式来显示这一天。 我的公式错了

  

var day_2 = Math.floor(time_diff / 24);           var day_1 = time_diff%24;

如何为此js代码添加日期功能,我看到很多倒计时器,但我想要这个。谢谢。

1 个答案:

答案 0 :(得分:0)

要获得时间差(以秒为单位)的天数,您可以使用:

day_2 = (time_diff / (60 * 60 * 24)) % 10;    
day_1 = Math.floor( (time_diff / (60 * 60 * 24)) / 10);  

把它放在这里:

var time_diff = Math.floor((target_time - current_time)/1000);

day_2 = (time_diff / (60 * 60 * 24)) % 10;    
day_1 = Math.floor( (time_diff / (60 * 60 * 24)) / 10);  

var second_2 = time_diff % 10; time_diff = Math.floor(time_diff/10);