【发布时间】:2014-07-04 03:12:52
【问题描述】:
我正在开发一个基于日历的仪表板小部件,我想在新的一天开始时自动更新它。但是,我似乎无法让它真正更新。它似乎在浏览器中运行良好。我还尝试手动将 setTimeout 延迟输入更短的时间(例如 1 或 2 分钟),这似乎可行。
这是我的代码:
function Calendar() {
var _self = this;
this.daytimer = null;
this.daytimerAction = function() {
_self.updateCurrentDate();
}
this.resetDaytimer();
}
Calendar.prototype.resetDaytimer = function() {
var today = new Date(),
tomorrow = new Date();
if (this.daytimer) {
clearTimeout(this.daytimer);
this.daytimer = null;
}
tomorrow.setDate(today.getDate() + 1);
tomorrow.setHours(0);
tomorrow.setMinutes(0);
tomorrow.setSeconds(1);
this.daytimer = setTimeout(this.daytimerAction, tomorrow.getTime() - today.getTime());
};
Calendar.prototype.updateCurrentDate = function() {
// Run code to update the day display
this.resetDaytimer();
};
有什么想法吗?我唯一能想到的是仪表板在仪表板未运行时暂停/取消 setTimeout。也许有办法在重新激活仪表板时重置超时?
【问题讨论】:
标签: javascript widget dashboard