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