【问题标题】:Apple Dashboard Widget setTimeout Not WorkingApple 仪表板小部件 setTimeout 不工作
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2014-04-02
      • 2013-11-05
      • 2023-03-12
      • 2017-01-12
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      相关资源
      最近更新 更多