嘿伙计们我正在尝试使用meteor.js中的模板,但是如何在给定时间后重新加载它们? ,例如,我想要一个名为" time"必须每秒重新加载以显示当前时间,也是模板" date"必须每分钟重新加载一次,因为日期可以改变,或者每5分钟左右必须重新加载.ics的日历,总是准确的。
我当前的代码如下:
<body>
<!-- TOP -->
<div class="top left">{{> time }}</div>
<div class="top mid"></div>
<div class="top right">{{> date }}</div>
</body>
<template name="time">
<div>{{ clock }}</div>
</template>
<template name="date">
<div>{{ date }}</div>
</template>
js:
if (Meteor.isClient) {
Template.time.helpers({
clock: function () {
var currentTime = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
currentHours = ( currentHours < 10 ? "0" : "" ) + currentHours;
currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
return currentHours + ":" + currentMinutes + ":" + currentSeconds;
}
});
Tempate.date.helpers({
date: function () {
var month = ["Januar", "Februar", "März", "April", "Mai", "Juni", "July", "August", "September", "October", "November", "Dezember"];
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
return d + " " + month[m] + " " + y;
}
});
}
请帮助我提高知识水平!
-Tim4497
答案 0 :(得分:4)
重新加载模板的最简单方法是提供和更改被动数据源。在您的情况下,这可能是时钟值。
var clock = new ReactiveVar(new Date());
Template.time.onCreated(function() {
Meteor.setInterval(function() {
clock.set(new Date());
}, 1000);
});
Template.time.helpers({
clock: function() {
var currentTime = clock.get();
...
},
});