The timer cannot be executed when the phone is black screen

Source: Internet
Author: User

Recently when writing the mobile phone page, found that when the phone side of the black screen, the current page timer stopped execution.

At first the idea was that H5 would also have the ability to listen to events of the onshow,onhide hook function of a small program. But after a variety of experiments, it was not possible to find such an event.

So, start to understand why the timer stops when the screen is black. After a series of Baidu, understand that it is iOS for security reasons.

You can'T. Web pages do not execute JavaScript unless (1) the page was frontmost and (2) Safari is actually active. Safari does not remain active when the device is asleep.If You think about it, ThisFunctionality would be a massive privacy breach. Imagine How do you'D feel if you visited a Web page so secretly tracked your location even when your device was off! You'D be very angry. EvenifYou could Do  ThisTechnologically, people may upsetifThey found outYou do it. Location Info isClosely guarded forReasons of privacy and physical safety (think about stalkers and your Web page.) That isWhy do youGetA dialog asking to use the location Manager, the first time youGetA location.

Now that iOS will force the timer off, it also finds a way to use the IFRAME to make JS timed even when the phone is locked.

Add an iframe to the page that needs the timer

<iframe src="background.html" style="display:none; "></iframe>

IFRAME Code

<! DOCTYPE html>"refresh" content="3 " />parent.run_function_from_core_page (); </script></body>

It's a good time to do timed tasks.

The timer cannot be executed when the phone is black screen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.