倒数计时器问题

时间:2015-12-09 00:56:05

标签: javascript jquery html timer

我一直在寻找谷歌上的倒计时器,似乎找不到一个。 我只是想知道是否有人能够提供帮助。 我得到了一个,但它显示错误的时间。

我希望它显示剩余的天,小时,分钟和秒。  继续我需要的计时器

http://pastebin.com/fQjyRFXw

它已经有了定时器代码,但是一切都错了,任何帮助都会很棒,谢谢

如果它有帮助,这里有一段Java代码

var count = <?= $time['a_time'] ?>;
var counter = setInterval(timer, 1000); //1000 will* run it every 1 second
function timer() {
    count = count - 1;
    if(count == -1) {
        clearInterval(counter); 
        return;
    }
    var seconds = count % 60;
    var minutes = Math.floor(count / 60);
    var hours = Math.floor(minutes / 60); 
    minutes %= 60; 
    hours %= 60; 
    document.getElementById("clock").innerHTML = hours + "hours " + minutes + "minutes and " + seconds + " seconds left"; 
}

2 个答案:

答案 0 :(得分:0)

好的,我看到了你的问题。存储在数据库中的a_time是一个Unix时间戳,因此当您倒计时时,您需要知道nowa_time之间的时间长度,而不仅仅是a_time。< / p>

试试这个:

var count = <?= $time['a_time'] ?>;
var now = Math.floor(new Date().getTime() / 1000);
count = count - now;
var counter = setInterval(timer, 1000); //1000 will* run it every 1 second
function timer() {
  count = count - 1;
  if(count == -1) {
    clearInterval(counter);
    return;
  }
  var seconds = count % 60;
  var minutes = Math.floor(count / 60);
  var hours = Math.floor(minutes / 60);
  var days = Math.floor(hours / 24);
  minutes %= 60;
  hours %= 24;
  document.getElementById("clock").innerHTML = days + "days " + hours + "hours " + minutes + "minutes and " + seconds + " seconds left";
}

答案 1 :(得分:0)

为什么不在codepen上使用其中一个man例子,比如这个漂亮的例子 http://codepen.io/anon/pen/VeLWdz

(function (e) {
    e.fn.countdown = function (t, n) {
    function i() {
        eventDate = Date.parse(r.date) / 1e3;
        currentDate = Math.floor(e.now() / 1e3);
        if (eventDate <= currentDate) {
            n.call(this);
            clearInterval(interval)
        }
        seconds = eventDate - currentDate;
        days = Math.floor(seconds / 86400);
        seconds -= days * 60 * 60 * 24;
        hours = Math.floor(seconds / 3600);
        seconds -= hours * 60 * 60;
        minutes = Math.floor(seconds / 60);
        seconds -= minutes * 60;
        days == 1 ? thisEl.find(".timeRefDays").text("day") : thisEl.find(".timeRefDays").text("days");
        hours == 1 ? thisEl.find(".timeRefHours").text("hour") : thisEl.find(".timeRefHours").text("hours");
        minutes == 1 ? thisEl.find(".timeRefMinutes").text("minute") : thisEl.find(".timeRefMinutes").text("minutes");
        seconds == 1 ? thisEl.find(".timeRefSeconds").text("second") : thisEl.find(".timeRefSeconds").text("seconds");
        if (r["format"] == "on") {
            days = String(days).length >= 2 ? days : "0" + days;
            hours = String(hours).length >= 2 ? hours : "0" + hours;
            minutes = String(minutes).length >= 2 ? minutes : "0" + minutes;
            seconds = String(seconds).length >= 2 ? seconds : "0" + seconds
        }
        if (!isNaN(eventDate)) {
            thisEl.find(".days").text(days);
            thisEl.find(".hours").text(hours);
            thisEl.find(".minutes").text(minutes);
            thisEl.find(".seconds").text(seconds)
        } else {
            alert("Invalid date. Example: 30 Tuesday 2013 15:50:00");
            clearInterval(interval)
        }
    }
    var thisEl = e(this);
    var r = {
        date: null,
        format: null
    };
    t && e.extend(r, t);
    i();
    interval = setInterval(i, 1e3)
    }
    })(jQuery);
    $(document).ready(function () {
    function e() {
        var e = new Date;
        e.setDate(e.getDate() + 60);
        dd = e.getDate();
        mm = e.getMonth() + 1;
        y = e.getFullYear();
        futureFormattedDate = mm + "/" + dd + "/" + y;
        return futureFormattedDate
    }
    $("#countdown").countdown({
        date: "1 April 2017 09:00:00", // Change this to your desired date to countdown to
        format: "on"
    });
});