Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / Javascript
Article

TimerGadget - A resumable timer gadget

Rate me:
Please Sign up or sign in to vote.
3.39/5 (10 votes)
23 Jan 20072 min read 56.6K   715   20   4
A Windows Sidebar gadget that provides a simple resumable timer

Sample Image - screenshot.jpg

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.

JavaScript
function getHTMLForNumber(param)
{
    var digit = 0;
    var htmlText = "";
    var index = 0;

    // Extract each digit from param and prepend the img tag 
    // pointing to the correct image.
    // The digits are extracted in reverse order, so prepend, don't append.
    while (param != 0)
    {
        digit = param % 10;
        param = Math.floor(param/10);
        htmlText = "<img src=\"" + images[digit] + "\" /> " + htmlText;
        index++;
    }

    // prepend zero(s) depending on the number of digits.
    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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer Atmel R&D India Pvt. Ltd.
India India
I'm a 27 yrs old developer working with Atmel R&D India Pvt. Ltd., Chennai. I'm currently working in C# and C++, but I've done some Java programming as well. I was a Microsoft MVP in Visual C# from 2007 to 2009.

You can read My Blog here. I've also done some open source software - please visit my website to know more.

Comments and Discussions

 
Questionno, dont install this (year 2020, Vista Ultimate) Pin
Member 1497733027-Oct-20 23:12
Member 1497733027-Oct-20 23:12 
AnswerRe: no, dont install this (year 2020, Vista Ultimate) Pin
OriginalGriff27-Oct-20 23:19
mveOriginalGriff27-Oct-20 23:19 
GeneralGadget help Pin
Brinda Murthy7-Feb-08 18:30
Brinda Murthy7-Feb-08 18:30 
GeneralCool gadget Pin
marcasselin1-May-07 2:37
marcasselin1-May-07 2:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.