簡體   English   中英

如何以字符串形式從我的收藏夾中獲取數據?

[英]How to get data from my Collection as a string?

我寫了一個日期選擇器,用戶可以使用引導程序在輸入字段內輸入日期:

    <template name="date">
      <form>
         <div class="input-group datetimepicker">
           <input name="date" class="set-due-date form-control" type="text"/>
           <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
         </div>
          ...
     </form>
    </template>

之后,我將其與表單的其他信息一起插入我的收藏中,並將用戶路由到新頁面。

    Template.date.events({
     'submit form':function(event,t){
      event.preventDefault();
      Questions.insert({
      closeDateDB: $('[name="date"]').val(),   
      ...
      ..
      },
    function (error,results){
      Router.go('decision', {_id:results})};
    });

我想為我的倒數計時器獲取closeDateDB的信息,並在endtime> currentTime時重定向用戶。

var timeinterval;

  Meteor.startup(function () {
    var endtime = *The closeDateDB of the submitted form!;*
    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) - Session.get('time');
    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) );


    if(t <= 0) {
      clearInterval(timeinterval);
      Router.go('finalpage');
   }
    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;
    }
  });


Template.date.onRendered(function() {
    this.$('.datetimepicker').datetimepicker({
      locale: 'en',
      format: 'MM/DD/YYYY'
    });
});

誰能幫我如何獲取插入的closeDateDB作為我的倒計時字符串? 我是Meteor的新手,非常感謝您的幫助。

  1. 您需要確保以一種時間格式保存closeDateDB ,以便可以直接將其與當前時間進行比較,即if ( closeDateDB > new Date() )如果closeDateDB是字符串, if ( closeDateDB > new Date() )將不起作用。
  2. 您正在以非常復雜的方式進行倒數計時。 使用remcoder:chronos程序包使時間本身變得被動,並節省大部分代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM