Safari JavaScript setTimeout 在最小化时停止 [英] Safari JavaScript setTimeout stops when minimized

查看:236
本文介绍了Safari JavaScript setTimeout 在最小化时停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与以下内容有关:
El Capitan 的 Safari 9.1
Safari 10 for Sierra

有谁知道当浏览器或选项卡失去焦点(主要是最小化)时,Safari 在 JavaScript 引擎中对 setTimeout() 做了什么?

我创建了一个简单的 JavaScript Web 应用程序,我将其加载到 Safari 中,该应用程序调用 JavaScript setTimeout(),该应用程序传递要在超时值之后执行的另一个函数.该函数将日期/时间打印到控制台,然后使用相同的超时值调用 setTimeout().

如果 Safari 选项卡失去焦点或浏览器被最小化,一段时间后 Safari 似乎停止执行 setTimeout 和后续要调用的函数,直到返回焦点,才会执行任何函数.就好像事件循环停止处理一样.

注意:没有函数调用丢失,它们只是暂停,并在浏览器重新获得焦点时重新启动.

我在 Firefox、Chrome 或 IE11 中没有注意到这一点.

提出这个问题的主要原因是该问题主要是在我维护的一个 Web 应用程序中观察到的,该应用程序使用 CometD 与服务器进行通信.CometD 库使用 setTimeout 每 30 秒执行一次返回服务器的请求.如果运行该应用程序的浏览器被最小化,则 CometD 似乎停止与服务器通信,直到浏览器再次最大化.

解决方案

你可能应该使用 setInterval 代替,因为根据 这个答案setInterval 仍然有效,即使它被限制为每秒 1 次.

<块引用>

WindowOrWorkerGlobalScope mixin 的 setInterval() 方法重复调用函数或执行代码片段,具有固定的每次通话之间的时间延迟.返回一个 intervalID.

语法 var intervalID = scope.setInterval(func, delay[, param1,参数 2, ...]);var intervalID = scope.setInterval(code, delay);参数

func 每延迟毫秒执行一次的函数.

code 可选语法允许您包含字符串而不是函数,每延迟毫秒编译和执行一次.这种语法不推荐使用 eval() a安全风险.

delay 时间,以毫秒为单位(千分之一第二),定时器应该在指定的执行之间延迟函数或代码.如果此参数小于 10,则值为 10用过的.请注意,实际延迟可能会更长;见原因延迟比指定时间长"在WindowOrWorkerGlobalScope.setTimeout() 示例.

param1, ..., paramN 可选 传递给一旦计时器到期,由 func 指定的函数.

<小时><块引用>

非活动标签中的超时限制为 >=1000 毫秒

从后台减少负载(和相关的电池使用量)选项卡,超时通常被限制为每次触发不超过一次非活动标签中的秒(1000 毫秒).

This is related to the following:
Safari 9.1 for El Capitan
Safari 10 for Sierra

Does anyone know what is Safari doing in the JavaScript engine with respect to setTimeout() when the browser or the tab loses focus (mainly, is minimized)?

I have created a simple JavaScript web application that I load in Safari that calls JavaScript setTimeout() that passes another function to be executed after the timeout value. The function prints the date/time to the console, then calls setTimeout() with the same timeout value.

If the Safari tab loses focus or the browser is minimized, after some time it seems Safari stops executing the setTimeout, and the subsequent function to be called, and until focus is returned, no function execution occurs. It's as if the event loop stops processing.

NOTE: No function calls are lost, they are only paused, and restarted when the browser regains focus.

I do not notice this in Firefox, Chrome or IE11.

Main reason this question is being posed is that the problem is mainly observed with a web application I am maintaining that uses CometD for communication back to the server. CometD library is using setTimeout to execute a request back to the server every 30 seconds. If the browser running the application is minimized, it seems CometD stops communicating with the server until the browser is maximized again.

解决方案

You should probably use setInterval instead, since according to this answer, setInterval still works, even if it is limited to 1 per second.

The setInterval() method of the WindowOrWorkerGlobalScope mixin repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. Returns an intervalID.

Syntax var intervalID = scope.setInterval(func, delay[, param1, param2, ...]); var intervalID = scope.setInterval(code, delay); Parameters

func A function to be executed every delay milliseconds.

code An optional syntax allows you to include a string instead of a function, which is compiled and executed every delay milliseconds. This syntax is not recommended for the same reasons that make using eval() a security risk.

delay The time, in milliseconds (thousandths of a second), the timer should delay in between executions of the specified function or code. If this parameter is less than 10, a value of 10 is used. Note that the actual delay may be longer; see "Reasons for delays longer than specified" in WindowOrWorkerGlobalScope.setTimeout() for examples.

param1, ..., paramN Optional Additional parameters which are passed through to the function specified by func once the timer expires.


Timeouts in inactive tabs clamped to >=1000ms

To reduce the load (and associated battery usage) from background tabs, timeouts are often clamped to firing no more often than once per second (1000 ms) in inactive tabs.

这篇关于Safari JavaScript setTimeout 在最小化时停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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