手機端黑屏時定時器無法執行

來源:互聯網
上載者:User

標籤:方法   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>

這樣可以愉快的進行定時任務了

手機端黑屏時定時器無法執行

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.