流星 - 如何显示自日期以来经过的时间
Meteor - How to display elapsed time since date
我想显示自特定日期以来经过的时间,在本例中为从数据库返回的记录创建日期。
模板简化:
<template name="shiftduty">
Uptime: {{uptime}}
</template>
以下仅在页面加载期间调用一次,但我需要每秒更新一次。处理这个问题最优雅的方法是什么?
Template.shiftduty.onCreated(function () {
Meteor.subscribe('shifts');
});
Template.shiftduty.helpers({
'uptime': ()=>{
var doc = Shifts.findOne({},{sort: {last_update:-1}});
var mins = (new Date().getTime() - doc.create_date.getTime())/60000;
return mins.toFixed(1) + ' mins ago';
}
});
我要用 ReactiveVar
再次尝试 Meteor.setInterval
吗?我似乎不能把它和那些放在一起。我不太热衷于用我见过的 Session.set/get
解决方案来填充全局。我觉得有点粗糙。
This include momentjs and reactiveVar libs. I hope this will be
helpfull
Template.yourTemplate.onRendered(function() {
Session.set('clock',0);
self.handle = Meteor.setInterval((function() {
var secunds = Session.get('clock');
Session.set('clock',secunds+1);
}), 1000);
});
Template.yourTemplate.helpers(function() {
uptime:function(){
var doc = Shifts.findOne({},{sort: {last_update:-1}});
if(doc){
var date = new Date(doc.create_date).getTime();
var currentDiff = moment().diff(date, 'seconds');
var seconds = new ReactiveVar(currentDiff);
var duration = seconds.get() + Session.get('clock');
return duration;
}
}
});
Template.yourTemplate.onDestroyed(function() {
Meteor.clearInterval(this.handle);
});
你可以这样格式化:hh/mm
function durFormated(duration) {
var h = duration/3600 ^ 0;
var m = (duration-h*3600)/60 ^ 0;
var s = duration-h*3600-m*60;
return {h:(h<10?"0"+h:h), m:(m<10?"0"+m:m)};
}
使用 ReactiveVar 和 SetInterval :-
Template.shiftduty.onCreated(function () {
Meteor.subscribe('shifts');
this.uptime = new ReactiveVar();
Meteor.setInterval(() => {
var doc = Shifts.findOne({},{sort: {last_update:-1}});
var mins = (new Date().getTime() - doc.create_date.getTime())/60000;
this.uptime.set(mins)
}, 1000);
});
Template.shiftduty.helpers({
uptime: () => Template.instance().uptime.get().toFixed(1) + " mins ago"
});
我想显示自特定日期以来经过的时间,在本例中为从数据库返回的记录创建日期。
模板简化:
<template name="shiftduty">
Uptime: {{uptime}}
</template>
以下仅在页面加载期间调用一次,但我需要每秒更新一次。处理这个问题最优雅的方法是什么?
Template.shiftduty.onCreated(function () {
Meteor.subscribe('shifts');
});
Template.shiftduty.helpers({
'uptime': ()=>{
var doc = Shifts.findOne({},{sort: {last_update:-1}});
var mins = (new Date().getTime() - doc.create_date.getTime())/60000;
return mins.toFixed(1) + ' mins ago';
}
});
我要用 ReactiveVar
再次尝试 Meteor.setInterval
吗?我似乎不能把它和那些放在一起。我不太热衷于用我见过的 Session.set/get
解决方案来填充全局。我觉得有点粗糙。
This include momentjs and reactiveVar libs. I hope this will be helpfull
Template.yourTemplate.onRendered(function() {
Session.set('clock',0);
self.handle = Meteor.setInterval((function() {
var secunds = Session.get('clock');
Session.set('clock',secunds+1);
}), 1000);
});
Template.yourTemplate.helpers(function() {
uptime:function(){
var doc = Shifts.findOne({},{sort: {last_update:-1}});
if(doc){
var date = new Date(doc.create_date).getTime();
var currentDiff = moment().diff(date, 'seconds');
var seconds = new ReactiveVar(currentDiff);
var duration = seconds.get() + Session.get('clock');
return duration;
}
}
});
Template.yourTemplate.onDestroyed(function() {
Meteor.clearInterval(this.handle);
});
你可以这样格式化:hh/mm
function durFormated(duration) {
var h = duration/3600 ^ 0;
var m = (duration-h*3600)/60 ^ 0;
var s = duration-h*3600-m*60;
return {h:(h<10?"0"+h:h), m:(m<10?"0"+m:m)};
}
使用 ReactiveVar 和 SetInterval :-
Template.shiftduty.onCreated(function () { Meteor.subscribe('shifts'); this.uptime = new ReactiveVar(); Meteor.setInterval(() => { var doc = Shifts.findOne({},{sort: {last_update:-1}}); var mins = (new Date().getTime() - doc.create_date.getTime())/60000; this.uptime.set(mins) }, 1000); }); Template.shiftduty.helpers({ uptime: () => Template.instance().uptime.get().toFixed(1) + " mins ago" });