標籤:方法 splay manage why ret device exe inf 手機
最近在寫手機端頁面時,發現當手機端黑屏時,當前頁面的定時器停止執行了。
一開始的思路是,h5也會有想小程式的onShow,onHide的鉤子函數的事件可以監聽。但是經過各種實驗也沒能找到這樣的事件。
so,開始瞭解為什麼黑屏時定時器停止。經過一系列的百度之後,明白在是ios的為了安全考慮。
You can‘t. Web pages do not execute javascript unless (1) the page is frontmost and (2) Safari is actually active. Safari does not remain active when the device is asleep.If you think about it, this functionality would be a massive privacy breach. Imagine how you‘d feel if you visited a web page that then secretly tracked your location even when your device was off! You‘d be very angry. Even if you could do this technologically, people may be upset if they found out you did it.Location info is closely guarded for reasons of privacy and physical safety (think about stalkers and your web page.) That is why you get a dialog asking to use the Location Manager the first time you get a location.
既然ios會強行將關閉定時器,於是也找到了方法,用iframe在即使手機被鎖定的時候也讓js定時執行。
在需要定時器的頁面加個iframe
<iframe src="background.html" style="display:none;"></iframe>
iframe代碼
<!DOCTYPE HTML><head><meta http-equiv="refresh" content="3" /></head><body><script>parent.run_function_from_core_page();</script></body></html>
這樣可以愉快的進行定時任務了
手機端黑屏時定時器無法執行