当窗口不清晰时,setTimeout会发生什么? [英] What happens to setTimeout when the window is out of focus?

查看:77
本文介绍了当窗口不清晰时,setTimeout会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,我需要在认证令牌到期之前在cordova应用上重新认证令牌.为此,我认为我会在auth令牌过期之前设置一个超时时间,以进行重新认证.

I have a situation where I need to reauthenticate a token on a cordova app before the authentication token expires. To do that I figured I'd set a timeout just before the auth token expires, to reauthenticate.

function authenticate() {
  var token = ... get token

  setTimeout(function() {
    .. try to reauthenticate
  }, token.expiresIn - 600*1000);
}

我可以看到的问题是-

  1. 应用程序睡眠时超时时间过去了.功能不触发?

  1. The timeout period passes while the app is sleeping. Function does not fire?

在应用程序休眠时暂停超时"(如果是这样的话).

The timeout "countdown" (if that's how it works) is paused while the app is sleeping.

这些都不是好方案.所以我的问题是,当应用程序失去焦点时,超时会发生什么?相反,我应该有10秒的时间间隔来检查这种情况下的到期时间吗?

Neither of these are good scenarios. So my question is, what happens to a timeout while the application is out of focus? Should I instead have a 10 second interval that checks the expiration for this scenario?

所以可以说令牌持续了4个小时.如果用户使用该应用程序一个小时,将其最小化2个小时然后返回,该函数将在一小时或3个小时后调用吗?这就是间隔的关键点,因此我可以相对较快地检查情况.

So lets say the token is for 4 hours. If the user uses the app for an hour, minimizes it for 2 hours and comes back, will the function call in an hour or 3 hours? This would be the point of the interval, so I can check the situation relatively quickly.

推荐答案

超时行为实际上取决于设备类型和操作系统版本.在某些情况下,任何到期"的计时器都会在应用程序启动后立即启动.在其他应用程序上(我相信当前的iOS就是这种情况),计时器在您的应用程序处于非活动状态时暂停,并在应用程序处于活动状态时恢复.

The timeout behavior really depends on the device type and OS version. On some, any timers that are "due" fire as soon as the application becomes active. On others (and I believe this is the case for current iOS), the timer is paused while your application is inactive and resumes when it becomes active.

对于长时间运行的计时器(即您的4个小时示例),您不能依赖 setTimeout(),因为在某些设备上,它不会考虑无效时间.您需要订阅科尔多瓦的恢复活动,然后重新计算并更新您的计时器.下面的 setLongTimeout()函数应在Cordoval中按预期方式运行.它未经测试,如果需要多次长时间超时,则需要扩展.

For a long-running timer (i.e. your 4 hours example) you can't rely on the setTimeout() because on some devices it won't account for the inactive time. You'll need to subscribe to Cordova's resume event and re-calculate and update your timers. The following setLongTimeout() function should behave as expected in Cordoval. It's untested and would need to be expanded if you need multiple long timeouts.

var longTimeoutId, longTimeoutDate, longTimeoutCallback;

// Use instead of `setTimeout()` for a long timeout in Cordova
function setLongTimeout(callback, delay) {
    if (longTimeoutId) {
        clearTimeout(longTimeoutId);
    }

    longTimeoutCallback = callback;
    longTimeoutDate = Date.now() + delay;

    longTimeoutId = setTimeout(function() {
        longTimeoutId = null;
        callback();
    }, delay);
}

document.addEventListener("deviceready", function() {
    document.addEventListener("resume", function() {
        if (longTimeoutId) {
            setLongTimeout(callback, longTimeoutDate - Date.now();
        }
    });
});

这篇关于当窗口不清晰时,setTimeout会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆