Thursday, July 9th, 2009
Lessons from Gmail: Using Timers Effectively
Neil Thomas of the Gmail for Mobile team keeps coming with great information on lessons learned. The latest discusses timers, and how to use them effectively (setTimeout/setInterval).
There are old questions here.... should you use lots of timers, or use one uber-timer that coordinates life? Here are some thoughts from Neil:
When I first started working on the new version of Gmail for mobile, the application used only a couple of timers. As we continued adding more and more features, the number of timers grew. We were curious about the performance implications: would 10 concurrent timers make the app feel slow? How about 100? How would the performance of many low-frequency timers compare to a single high-frequency timer?
To answer these questions, we conducted a few experiments. We injected some extra code into a development build of Gmail that created a lot of different timers, each of which just performed some simple calculations. Then we fired up the app on an iPhone 3G and Android G1 (both running the latest version of their respective firmware) and observed the performance. Note that although we could have just created a separate test page for this, we chose to inject the code right into our app so we could see how fast it would be to read and process mail with all those timers running.
Here's what we learned. With low-frequency timers — timers with a delay of one second or more — we could create many timers without significantly degrading performance on either device. Even with 100 timers scheduled, our app was not noticeably less responsive. With high-frequency timers, however, the story was exactly the opposite. A few timers firing every 100-200 ms was sufficient to make our UI feel sluggish.
This led us to take a mixed approach with the timers we use in our application. For timers that have a reasonably long delay, we just freely create new timers wherever they are needed. However, for timers that need to execute many times each second, we consolidate all of the work into a single global high-frequency timer.
He then goes on to discuss the nice trick to tell if an application has come back from a sleeping state (and thus, maybe you want to call home and sync):
One neat application of a high-frequency timer is to detect when the device has been woken from sleep. When your application is put to sleep — either because the device is put into sleep mode or because the user has navigated away from the browser to another application — time, as perceived by your app, pauses. No timers fire until the user navigates back to your app and it wakes up. By keeping a close eye on the actual time that elapses between high-frequency timer ticks, you can detect when the app wakes from sleep.
First, create a high-frequency timer, as described above. In your timer, call a function that keeps track of the time between ticks:
JAVASCRIPT:
// The time, in ms, that must be "missed" before we // assume the app was put to sleep. var THRESHOLD = 10000; var lastTick_; detectWakeFromSleep_ = function() { var now = new Date().getTime(); var delta = now - this.lastTick_; if (delta> THRESHOLD) { // The app probably just woke up after being asleep. fetchUpdatedData(); } lastTick_ = now; };Now your users will always have the latest data available when they return to your app without having to manually perform a refresh!
We recently ran into timer fun, as Ben mentioned on his directory post. He put together a nice little timing framework that is DOM agnostic that lets you have some fun with setting frameworks. There are other frameworks out there of course, especially when you get into the DOM (dojo.Animation can be used in the abstract, and there are then DOM specific impls on top).
Robert O'Callahan has been playing around with the idea of a framework in the browser too, with mozRequestAnimationFrame. Fun :)












this is a very interesting read for anyone doing continuous updates in the browser, and i recommend reading both http://googlecode.blogspot.com/2009/07/gmail-for-mobile-html5-series-using.html and http://ejohn.org/blog/how-javascript-timers-work/ .
i’ve found the single most important piece of advice in this field is, “when you can not be very sure that the code called by setInterval(f,dt0) will finish in clearly less than dt0 milliseconds, consider to use setTimeout(f,dt1) instead (from within f itself to keep the process running)”. this will give at least dt1 milliseconds to your other browser processes to execute so a freeze becomes less likely. you may want to check elapsed time before calling setTimeout from within f so you can add more delay in case execution was swift.
With the changing trend of the world, wedding apart from white,Princess Strapless Satin Evening Dress ivory, beige and other traditional colors,are also increasingly popular in recent years, ghetto prom dressespink wedding package,gothic prom dresses such as pink, pink orange, baby blue, purple night, light green and light silver-gray, very soft and pleasing to wedding hair accessoriesthe eye; if you have the courage to try, dark green, purplish red, deep purple, decorated in pink wedding dress, the formation of rich colors,the dress, the effect is very special;Sash Lace Satin Flower Girl Dress, laden with flour colored silk flowers, butterflies, in order to add color.wedding dressIn fact, the wedding is the color of lesser importance, the most important prerequisite is to match the bride’s complexion. Oriental deep and yellowish color, wearing white Mermaid Satin Organdie Wedding Dress,will appear dull, wearing ivory would be more harmonious and natural, baby blue, wedding accessoriespurple night can not be coordinated with the yellow, but pink orange, light green with yellowish color match. As for the skin and rosy, or bronze skin, wearing white will look great, wedding dress
A wedding dresses is always
expensive. Every couple will agree on this.When you are planning for
your wedding, you will probably find that every item can be very
expensive. This is especially true when it comes to the wedding dress.
You may think that there is no way for you to get a Wedding gown dresswhich is
elegant and at the same time cheap. However, I can tell you that there
are some ways to save money on this item.
So, the question here is how to make your white wedding dressinexpensive.
In fact, one thing you need to understand is that it is totally
possible for you to find a cheap
wedding dress which is also elegant and beautiful. You do not need
to sacrifice the quality just because you will need to search for cheap wedding gowns items.
As a matter of fact, one of the ways to get a bridal gowns is to wear the one
your mother wore when she got married. The point here is that you will
be wearing a bridal dresses
item and it will certainly be very stylish.
The principal concern whenever lifting heavy objects on an uneven
surface is to ensure stability. The basic wedding dresses esign premise of
a cantilever hoist is to include a heavy counter weight that opposes
the load and ensures longitudinal seostability in use.
This use of a counterweight is backed up by a relatively wide chassis
on fully lockable 8 inches by 2 Electric hoist inches phenolic
casters.Whenever you need to be able to electric hoist or lift a
weighty object from the floor onto a higher level, having a lifting
system chain hoist that you
can rely on is essential. Unfortunately, there are always situations
where it is simply not possible to use conventional chain block lifting gear such as
jibs or fork lifts, and under these situations, when it is Snatch Block necessary to be able to raise a
weight quickly, then a cantilever hoist is often the best choice.
Cheap wedding dresses