【发布时间】:2014-06-25 19:59:54
【问题描述】:
编辑:我认为我应该让事情更明显。
我要做的是使显示从帖子提交日期开始的“时间前”的功能每分钟自动刷新一次,这样即使没有重新渲染模板也能保持相对准确。
我想在我的模板中自动更新我的 timeago 值,但它不起作用。
根据对类似问题 (https://stackoverflow.com/a/17933506) 的回答,我尝试使用反应函数设置我的代码
这是我的代码:
var timeAgoDep = new Deps.Dependency(); // !!!
var timeAgo;
var timeAgoInterval;
Template.postItem.created = function() {
function getTimeago() {
//var now = new Date();
timeAgo = moment(this.submitted).twitter();
timeAgoDep.changed(); // !!!
};
getTimeago(); /* Call it once so that we'll have an initial value */
timeAgoInterval = Meteor.setInterval(getTimeago, 5000);
};
Template.postItem.posted = function() {
timeAgoDep.depend(); // !!!
return timeAgo;
};
Template.postItem.destroyed = function() {
Meteor.clearInterval(timeAgoInterval);
};
我很确定问题来自 this.submitted 因为如果我分配 timeAgo = now 例如,它将显示时间并按预期更新。
我也知道 moment(this.submitted).twitter() 工作正常,因为当我所做的只是通过帮助程序返回它时,它就可以工作。
【问题讨论】:
标签: javascript meteor momentjs