
Introduction
This gadget is a simple resumable timer with a digital clock face. It also includes a textbox to allow users to add text that could describe what is being timed. The Vista gadget competition and the desire to find how gadgets work were the main motivations for this article.
Background
I've always thought a resumable timer would be very handy to measure the actual amount of time I spend doing something, excluding various distractions. In fact, I was mildly surprised to find it missing from the standard Vista gadgets that ship with the operating system. Along came the Vista Gadget competition and I thought this would be a nice fit. Not being very skilled in HTML/JavaScript, I decided to limit myself to a simple, no frills timer that can be started, stopped and resumed. I also decided to add a textbox that will allow the user to enter text, presumably related to what is being timed.
Installation
To install the gadget, all you need to do is to unzip Timer.zip and open the Timer.gadget file. Timer.gadget is a simple ZIP file containing the gadget files, with the extension renamed from .zip to .gadget. Vista takes care of putting the gadgets in the correct directory (%USERPROFILE%/AppData/Local/Microsoft/Windows Sidebar/Gadgets) automatically. Really simple installation mechanism indeed!
How it works
The code is all JavaScript and uses three global variables seconds
, minutes
and hours
to store the time elapsed so far, with the hours variable rolling over to zero when going from 99 to 100. The actual timer is created via window.setInterval
and is stopped using window.clearInterval
by passing the timerId
returned by the first function. All pretty straight, normal JavaScript code. The only slightly interesting part is the mapping of the seconds, minutes or hours to images that are displayed on the digital clock face. To do this, I created separate image files (.gif) for each digit (0-9) and loaded their paths in an array (images
). The following piece of code takes an integer value and generates the corresponding HTML necessary to display it in the image format.
function getHTMLForNumber(param)
{
var digit = 0;
var htmlText = "";
var index = 0;
while (param != 0)
{
digit = param % 10;
param = Math.floor(param/10);
htmlText = "<img src=\"" + images[digit] + "\" /> " + htmlText;
index++;
}
while (index<2)
{
htmlText = "<img src=\"" + images[0] + "\" /> " + htmlText;
index++;
}
return htmlText;
}
How to use it
To start the timer, hit the Start button. Before starting, you can optionally enter some text in the textbox above the buttons, describing the task you are timing. Once started, the timer starts ticking. To pause the timer, hit the Pause button. If you want to reset the timer back to zero, hit the Reset button. Note that the Reset button also stops the timer.
History
- 20:57 23-01-2007 - Initial submission