2014-05-15 24 views
0

我正在开发基于日历的仪表板小部件,我想在新的一天开始时自动更新这个小部件。但是,我似乎无法让它实际更新。它似乎在浏览器中正常运行。我也尝试手动输入setTimeout延迟到更短(如1或2分钟),似乎工作。Apple仪表板小部件setTimeout不起作用

这是我的代码:

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。当仪表板重新激活时,可能有重置超时的方法吗?

回答

0

想通了。 Apple documentation概述了我可以用来重新启动计时器的widget.onshow事件以及直接呼叫updateCurrentDate()。还有一个可用于暂停计时器的事件widget.onhide