【问题标题】:Countdown Timer Problems倒数计时器问题
【发布时间】:2015-12-09 00:56:05
【问题描述】:

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

我希望它显示剩余的天数、小时数、分钟数和秒数。 这是我需要的计时器

http://pastebin.com/fQjyRFXw

那里已经有计时器代码了,但都是错误的,任何帮助都会很棒,谢谢

如果有帮助,这里是 Java 代码的 sn-p

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"; 
}

【问题讨论】:

  • a_time的格式是什么?
  • Unix 我很确定
  • 好的,如果你在 timer() 函数中放置一个 console.log(count); 并检查日志,也许会有所帮助。
  • 是的,也许,检查 pastebin 链接,会给你更多的想法。

标签: javascript jquery html timer


【解决方案1】:

好的,我明白你的问题了。数据库中存储的a_time 是一个Unix 时间戳,因此当你倒计时时,你需要知道nowa_time 之间的时间,而不是只有a_time

试试这个:

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";
}

【讨论】:

  • 谢谢你的回答,现在我唯一的问题是它没有正确计算天数,它会说 28 小时,而不是 1 天 4 小时。这位朋友有什么想法吗?
  • 谁能帮我解决这个问题?
  • 谢谢你,我现在测试一下并回复你
【解决方案2】:

为什么不使用 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"
    });
});

【讨论】:

  • 干杯,我会试试这个。
  • 如果没有成功,还有很多其他的。我刚刚搜索了倒计时日期。
  • 谢谢,我一定会看看队友,这个看起来像.html 对吗?我真的只是在寻找一个简单的解决方法来解决当前的问题,我只是没有正确计算天数和小时数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多