Archive for July, 2005

Serenity

Sunday, July 31st, 2005

Kinda surprised M1K3¥ hasn’t blogged this. A couple of weeks ago there was a preview screening of serenity in Melbourne.

serenity

Serenity can be viewed as a standalone movie, but is based on the TV series Firefly, by Joss Whedon (of Buffy & Angel fame). Unfortunately Firefly was cancelled mid-season (after being partially aired out of order!), but the whole season is available on DVD.

To keep this vaguely on-topic, one of the interesting aspects in Firefly/Serenity is the mixture of English & Chinese (Mandarin I believe). This is largely illustrated by the use of Chinese to replace swearing, but it is indicated that in the Firefly universe (set in the year 2517) English/Chinese bilinguality is the norm.

No spoilers here, but I will say I enjoyed the movie a lot (more than most movies I’ve seen this year).
Serenity is out in Australia on September 30. Go see it!

iTunes/gmail dynamic counters…

Sunday, July 10th, 2005

Michael has an explanation of how the itunes sale counter works using AJAX.
This reminded me of the storage space count on the gmail page (if you have an account on gmail you may not see the dynamic counter unless you logout).

I won’t give a full walkthrough, as I’ve only spent about 30 seconds looking at the code. But the gmail counter is a bit simpler than the apple version, probably due to the fact that google completely control the rate of growth. This allows them to rely only on static data served with the page (they’ve scheduled/estimated the amount of storage that will be available at some times in the future), and then they simply use linear interpolation based on the current time.

For example, my page was served with the static data defining the [time, capacity] pairs:


    var CP = [
         [ 1117609200000, 2250 ],
         [ 1120201200000, 2350 ],
         [ 1122879600000, 2450 ]
    ];

For the curious, these entries correspond to the start of June, July and August respectively. I expect as it approaches August, they will start serving pages with an entry for September.

The update code simply finds the relevant entry in the CP array, and then interpolates based on the current time.


    function updateQuota() {
      if (!quota) {
        return;
      }

      var now = (new Date()).getTime();
      var i;
      for (i = 0; i < CP.length; i++) {
        if (now < CP[i][0]) {
          break;
        }
      }
      if (i == 0) {
        setTimeout(updateQuota, 1000);
      } else if (i == CP.length) {
        quota.innerHTML = 'Over ' + CP[i - 1][1];
      } else {
        var ts = CP[i - 1][0];
        var bs = CP[i - 1][1];
        quota.innerHTML = format(((now-ts) / (CP[i][0]-ts) * (CP[i][1]-bs)) + bs);
        setTimeout(updateQuota, 1000);
      }
    }

This is a good illustration how precomputation can avoid trips to the server.
Although, since it doesn’t use a roundtrip, I guess it doesn’t count as AJAX at all…