【问题标题】:Making countdown function in jQuery在jQuery中制作倒计时功能
【发布时间】:2012-03-25 20:04:45
【问题描述】:


我是新手,需要你的帮助。我在找出天、小时、分钟和秒时遇到问题。我不知道要在几秒后更新分钟数将达到 60 小时和天数相同。
我正在尝试以天/小时/分钟/秒的格式倒计时到给定日期。
Here is the link of what I've done so far.
提前谢谢! :)

【问题讨论】:

  • 看起来这是一道数学题而不是编程题?

标签: javascript gettime jquery


【解决方案1】:

您为什么不使用众多可用的插件之一来解决问题,请考虑以下链接:

http://keith-wood.name/countdown.html

这个列出了许多用于相同目的的插件,使用它们中的任何一个并微笑:) http://www.tripwiremagazine.com/2012/01/jquery-countdown-scripts.html

【讨论】:

    【解决方案2】:
    var seconds = 1355998990 - new Date().getTime(), // whatever
    minutes = Math.floor(seconds/60),
    hours = Math.floor(minutes/60),
    days = Math.floor(hours/24);
    
    seconds %= 60;
    minutes %= 60;
    hours %= 24;
    
    console.log("d: " + days + " h: " + hours + " m: " + minutes + " s: " + seconds);
    

    这样可以吗?

    还请务必查看this;也许你可以在那里得到一些灵感。

    【讨论】:

    • 时钟真棒!还有,什么是 console.log() ?
    • console.log() 将直接登录到 Chrome 调试控制台和 Firebug 控制台。为了快速检查变量值是什么:)
    【解决方案3】:

    我修改了您的count_execution 函数并添加了一些cmets。它应该按照你的要求去做。

    function count_execution() {
      // target date of event
      var eventDate   = new Date(settings.date);
      // now
      var currentDate = new Date();
      // get date diff *in milliseconds*
      var diff   = Math.abs(currentDate - eventDate);
    
      // compute days left
      // 24h * 60m * 60s * 1000 = 86400000
      var days = Math.floor(diff / 86400000);
      diff = diff % 86400000;
    
      // compute hours left
      // 60m * 60s * 1000 = 3600000
      var hours = Math.floor(diff / 3600000);
      diff = diff % 3600000;
    
      // the same for minutes
      var minutes = Math.floor(diff / 60000);
      diff = diff % 60000;
    
     // finally, seconds
     var seconds = Math.floor(diff / 1000);
    
     $this.find('#days').text(days);
     $this.find('#hours').text(hours);
     $this.find('#mins').text(minutes);
     $this.find('#secs').text(seconds);
    }
    

    希望对你有帮助。

    【讨论】:

    • 是的,我得到了我想要的,但是有没有更简单的方法来使用 jQuery 获取小时、分钟和秒?
    • 我认为您最好的选择可能是使用像@Mohammed 建议的 jQuery 插件。严格来说,我最近发现了Moment.js 的日期操作和格式设置,它对我非常有用。
    猜你喜欢
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 2012-01-09
    • 2013-03-18
    • 1970-01-01
    • 2019-10-20
    相关资源
    最近更新 更多