Coding JavaScript for Mobile Browsers (part 6) - Microsoft Certification Examples, exer... - 0 views
-
In mobile browsers, you need to be especially careful about using timers because of the battery consumption. If you need to use many high-frequency timers at the same time, try to manage them using only one timer that will launch different behaviors from the same process.
-
The first question we need to ask ourselves is, what happens when our web page goes to the background because the user switches focus to another application (in multitasking operating systems) or opens or browses to another tab or window? Another problem is what happens when the phone goes to sleep (because of the user’s inactivity while the script is executing). The behavior of timers can be a little tricky in these situations.Yet another problem is that timers execute on the same thread as the main script. If our script is taking too much processor time (a normal situation with large scripts on low- and mid-end devices), our timers will be delayed until some spare execution time is found.If we use a low frequency for the timer (for example, 10 milliseconds), the timer will generally have problems meeting the timetable.Remember that the JavaScript execution time depends a lot on the device hardware and the browser’s engine. Even if they’re running the same operating system, like Android, execution times can differ: for example, an HTC G1 will be much slower than a Nexus One with a 1-Ghz processor.
-
As shown in Figure 4, the real times are very different on different devices. On low- and mid-end devices, if they work at all, the result is far from our 200 ms intention—some low-end devices don’t even accept timers with a frequency of less than 1 second.
- ...1 more annotation...