Jason Vertucio

I did a thing!

Javascript: Timer Class

Today, I needed a Timer class for the site I’m working on. The project that I’m doing — as usual — has gone through yet another change. One chart needed to animate differently. Since iPad slideshows are apparently my bag, baby, I couldn’t use my normal jQuery magic. So, I’ve been relying on 3D-accelerated -webkit- animation. Ah, Webkit. The only proprietary CSS-prefix I actually am forced to use. Anyway, since I could not control the animation using JavaScript, I was not able to control a counter ticking up one by one. It would just start with the zero, and end at the final number. Thanks to the cool people at dailycoding.com, I was able to easily set up a method to update the counter. No extra brain-power needed.

The way it looks is easy.

The code eventually looked like this. Discerning eyes may note differences between the one at Daily Coding and here. I made the constructor a little more useful, taking in the interval and callback. I also renamed it, because heaven forbid I actually keep the term Timer.


#line-graph {
 background:url('img/chart-lines.png') no-repeat left top;
 -webkit-transition:all ease-in-out 0.8s;
#line-graph.grow {
var dailyCodingTimer = function(interval,callback) {
    // Property: Frequency of elapse event of the timer in millisecond
    if (typeof(interval) == 'undefined') thisObject.interval = 1000;
        else this.interval = interval;
    // Property: Whether the timer is enable or not
    // Event: Timer tick
   	if (typeof(callback) == 'undefined') this.tick = null;
		else this.tick = callback;
    // Member variable: Hold interval id of the timer
    var timerId = null;
    // Member variable: Hold instance of this class
    var thisObject;
    // Function: Start the timer
    this.Start = function() {
        this.enabled = true;

        thisObject = this;
        if (thisObject.enabled) {
            thisObject.timerId = setInterval(
            function() {
            }, thisObject.Interval);
    // Function: Stops the timer
    this.Stop = function()
        thisObject.enabled = false;
// Remember to include the timer Class from dailycoding
animateChart : function() {
  util.removeClass ( slide.element.lettersGained , 'hidden' );
  setTimeout ( function() {
    util.addClass ( slide.element.graphLine, 'grow');			
    // Create Timer Instance and Set Timer
    slide.timerObj = new DailyCodingTimer(10,slideUpdateNumbers);
  } , 500 );
updateNumbers : function () {
	var linePosition = slide.element.graphLine.clientWidth + 1;
	var importantN = linePosition / ( maxWidth - importantMax );
	var newNumber = importantN.toFixed(1);
	slide.element.numberCode.innerHTML = newNumber;

	if ( i >= 320 ) this.Stop();

(In the above code, the slide.elements refer to a getElementById item, and util.addClass and util.removeClass functions add and remove classes, and maxWidth and importantMax are defined elsewhere.)

Anyway, what happens in the above is that the line chart is masked off by the CSS. Once the animateChart method is called, the mask’s width grows and grows until the full line chart is revealed. The timer is used to call an update method on whatever numbers will be updated. By pointing object.tick to the function object you want to run, and setting the interval, it will run as often as you like. The version I used (such a fan of setTimeout I am) wasn’t producing the desired results, so thank you, Daily Coding, for the much-needed help.

Peruse Daily Coding for more great Javascript and jQuery tips. A note of warning, though. They call themselves “daily” coding, but it’s more like . . . whenever they remember to update the site.

(It’s not that daily!)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.