设置倒数计时器的开始和结束日期? (使用Meteor.js)

时间:2022-09-26 11:14:25

Right now I have a countdown timer working from the current system time counting down to and endtime, but I want to be able to create a start time as well, and I'm not sure how to do that. Here is my javascript right now:

现在我有一个倒数计时器,从当前系统时间倒计时到结束时间,但我希望能够创建一个开始时间,我不知道该怎么做。这是我现在的javascript:

if (Meteor.isClient) {
var timeinterval;

Meteor.startup(function () {
var starttime = 'February 19 2016 22:00:00 UTC-0600'
var endtime = 'February 21 2016 10:00:00 UTC-0600';
timeinterval = setInterval(function () {
  Meteor.call("getCurrentTime", function (error, result) {
    Session.set("time", result);
    var t = getTimeRemaining(endtime);
    Session.set("t", t);
  });
}, 1000);
});

function getTimeRemaining(endtime){
var t = Date.parse(endtime) - starttime;
var seconds = ("0" + Math.floor( (t/1000) % 60 )).slice(-2);
var minutes = ("0" + Math.floor( (t/1000/60) % 60 )).slice(-2);
var hours = ("0" + Math.floor( (t/(1000*60*60)) % 24 )).slice(-2);
var days = Math.floor( t/(1000*60*60*24) );

console.log(t)
if(t <= 0)
  clearInterval(timeinterval);

return {
  'total': t,
  'days': days,
  'hours': hours,
  'minutes': minutes,
  'seconds': seconds
};

}

Template.countdown.helpers({
t: function () {
  return Session.get("t");
}
});

Template.body.helpers({
ended:function () {
  console.log(Session.get("t").total <= 0);
  return Session.get("t").total <= 0;
}
});
}

if (Meteor.isServer) {
 Meteor.startup(function () {

});

Meteor.methods({
 'getCurrentTime': function (){
   return Date.parse(new Date());
 }
 });
 }

I'm really not sure where to go from here, as I don't have any experience with javascript. Any help would be great. Thanks!

我真的不确定从哪里开始,因为我没有任何javascript经验。任何帮助都会很棒。谢谢!

2 个解决方案

#1


0  

sounds to me if I understand correctly, you want to either countdown to start (if start is later than now), or countdown to end (if it's already started).

听起来如果我理解正确,你想倒计时开始(如果开始比现在晚),或倒计时结束(如果它已经开始)。

Or maybe you just want to not show the timer unless it's started, not clear from your question.

或者你可能只是想不显示计时器,除非它已经开始,不清楚你的问题。

Either way, just modify your getTimeRemaining function to take in start time parameter as well, and check whether if now is less than start, if so then count against start instead of end.

无论哪种方式,只需修改你的getTimeRemaining函数以获取开始时间参数,并检查现在是否小于start,如果是,则计算start而不是end。

function getTimeRemaining(starttime, endtime){
  // translate JSdate to Ms before you can compare
  var nowInMs = new Date().getTime();
  var startTimeMs = starttime.getTime();
  
  if (nowInMs < startTimeMs) {
    ... do what you wanted, maybe compare diff and count from there
    ... or just not show the timer
  } else {
    ... do what you were doing
  }

Hope that helped :)

希望有帮助:)

#2


0  

If you're talking about creating a 'time since XYZ date' timer, I'd use this package: https://github.com/copleykj/meteor-livestamp

如果您正在谈论创建“自XYZ日期以来的时间”计时器,我将使用此软件包:https://github.com/copleykj/meteor-livestamp

This will provide you with functionality to say how long it's been since something happened (e.g. 3 minutes ago), just like with Facebook comments.

这将为您提供一些功能,可以说明事情发生后的持续时间(例如3分钟前),就像Facebook评论一样。

#1


0  

sounds to me if I understand correctly, you want to either countdown to start (if start is later than now), or countdown to end (if it's already started).

听起来如果我理解正确,你想倒计时开始(如果开始比现在晚),或倒计时结束(如果它已经开始)。

Or maybe you just want to not show the timer unless it's started, not clear from your question.

或者你可能只是想不显示计时器,除非它已经开始,不清楚你的问题。

Either way, just modify your getTimeRemaining function to take in start time parameter as well, and check whether if now is less than start, if so then count against start instead of end.

无论哪种方式,只需修改你的getTimeRemaining函数以获取开始时间参数,并检查现在是否小于start,如果是,则计算start而不是end。

function getTimeRemaining(starttime, endtime){
  // translate JSdate to Ms before you can compare
  var nowInMs = new Date().getTime();
  var startTimeMs = starttime.getTime();
  
  if (nowInMs < startTimeMs) {
    ... do what you wanted, maybe compare diff and count from there
    ... or just not show the timer
  } else {
    ... do what you were doing
  }

Hope that helped :)

希望有帮助:)

#2


0  

If you're talking about creating a 'time since XYZ date' timer, I'd use this package: https://github.com/copleykj/meteor-livestamp

如果您正在谈论创建“自XYZ日期以来的时间”计时器,我将使用此软件包:https://github.com/copleykj/meteor-livestamp

This will provide you with functionality to say how long it's been since something happened (e.g. 3 minutes ago), just like with Facebook comments.

这将为您提供一些功能,可以说明事情发生后的持续时间(例如3分钟前),就像Facebook评论一样。