是否有移动设备的网络浏览器中的线程睡眠方面的任何标准来衡量?线程、睡眠、器中、方面

2023-09-12 01:22:43 作者:ㄡ冇誰會吢疼√

给定以下的jsfiddle,这是一个简单的增量计数器

http://jsfiddle.net/C93ms/6/

....如果我访问上面使用移动设备(智能手机或平板电脑的参数的缘故)的URL,计数器开始递增如你所期望只要有JavaScript的支持,那么它看来,如果我$ PSS的主页按钮p $,或者点击一次电源按钮以关闭屏幕(但保持手机开机),那么脚本将停止运行,计数器停止计数。这是我希望发生,我AP preciate的原因,保留的电池寿命是在移动设备上非常重要的,所以它是有道理的UI线程睡眠,或类似的。一旦你重温浏览器,计数器继续递增。在现实世界中,决定使用JavaScript超时时间的网站将不会超时,尽管非活动时间,我假设。

我也假设这会因设备而异,固件,软件甚至是 - 就是我的尝试的这里确定是是否有一个标准方法或默认行为建成,这和移动开发框架中的设备的行为的任何形式的一致性。

我不完全确定我问一个的好问题的这里,但我挣扎着从SO发现100%的相关信息,或我不太知道问题是什么我要问搜索时。

感谢

解决方案

没有JavaScript框架,可以停止执行或改变底层JS引擎的行为。他们将不能够影响的setTimeout

然而,行为standardisized在对 WindowTimers 接口(这并不意味着它是这样实现的)。在那里,你会发现说明:

  如何在移动设备上调试html5开发的网页

这个API并不保证计时器将如期运行完全相同。延迟由于CPU负载,其他任务等,都是可以预期的。

和,更明确的:

  

9)任选,等待进一步的用户代理指定的时间长度。

          

请注意:这是为了让用户代理垫超时根据需要优化设备的电源使用情况。例如,某些处理器具有一个低功率模式,其中定时器的粒度降低;在这样的平台上,用户代理可以减缓计时器,以适应需要的处理器使用更精确的方式与其相关联的高用电量的这种安排吧。

  

您可以看到在桌面浏览器,其实现的4ms的最小超时这种行为也(读的解释的MDN )。因此,它是合法的每个设备/软件/固件停止执行等,如果他们只认为这是必要的。

您可能也想看看在 WindowAnimationTiming 草案。

如果你使用的setInterval / 的setTimeout 在动画/时钟/等,经常测量真正经过时间与 日期对象(例如通过 Date.now())。

Given the following jsFiddle, which is a simple incrementing counter

http://jsfiddle.net/C93ms/6/

....if I visit the url above using a mobile device (smartphone or tablet for the sake of argument), the counter starts incrementing as you'd expect provided there is JavaScript support, then it appears that if I press the "Home" button, or click the power button once to turn off the screen (but keep the phone powered on) then the script will stop running and the counter stops incrementing. This I expect to happen and I appreciate the reasons why as reserving battery life is hugely important on a mobile device, so it makes sense that the UI thread sleeps, or similar. Once you revisit the browser, the counter continues incrementing. In the real world, websites that determine timeout period using JavaScript would not timeout despite the inactivity period, I am assuming.

I am also assuming that this will vary by device, by firmware, by software even - what I'm trying to ascertain here is whether there's a standard approach or default behaviour built into mobile development frameworks for this and any form of consistency in how the devices behave.

I'm not totally sure I've asked a good question here, but I've struggled to find 100% relevant information from SO, or I don't quite know what the question is I need to ask when searching.

Thanks

解决方案

No JavaScript framework can stop the execution or change the behaviour of the underlying JS engine. They will not be able to influence setTimeout.

Yet, the behaviour is standardisized in the current HTML5 draft on the WindowTimers interface (which does not mean it was implemented like that). There you will find the note:

This API does not guarantee that timers will run exactly on schedule. Delays due to CPU load, other tasks, etc, are to be expected.

and, even more explicit:

9) Optionally, wait a further user-agent defined length of time.

Note: This is intended to allow user agents to pad timeouts as needed to optimise the power usage of the device. For example, some processors have a low-power mode where the granularity of timers is reduced; on such platforms, user agents can slow timers down to fit this schedule instead of requiring the processor to use the more accurate mode with its associated higher power usage.

You can see such behaviour also on desktop browsers, which implement a minimum timeout of 4ms (read explanation on MDN). So, it is legitimate for every device/software/firmware to stop such execution if they only think it would be necessary.

You might also want to have a look at the WindowAnimationTiming draft.

And if you do use setInterval/setTimeout in animations/clocks/etc, always measure the really elapsed time with Date objects (e.g. via Date.now()).

 
精彩推荐
图片推荐