在jQuery中进行倒计时功能

时间:2012-03-08 10:15:53

标签: javascript gettime jquery


我是新手,需要你的帮助。我发现天,小时,分钟和秒都有问题。我不知道在几秒钟之后会更新几分钟的时间达到60分钟,并且与小时和天数相同 我试图以天/小时/分钟/秒的格式倒计时 Here is the link of what I've done so far.
Thnx提前! :)

3 个答案:

答案 0 :(得分:3)

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;也许你可以在那里得到一些灵感。

答案 1 :(得分:2)

为什么不使用可用的众多插件中的一个来做这个技巧,请考虑以下链接:

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

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

答案 2 :(得分:1)

我修改了您的count_execution功能并添加了一些评论。它应该按照你的要求做。

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

希望它有所帮助。