Quantcast
Channel: UNDERPRISE » Development
Viewing all articles
Browse latest Browse all 3

countdown.js

$
0
0

The Adobe Creative Jam needed a countdown timer so that those involved with the contest would know how much time they had remaining. I created a simple Javascript library to handle keeping track of the time remaining.

The source code and an example of how to use it are available here: https://github.com/schmidtkevina/countdown.js

Using the Countdown.js Library

Creating a new countdown time is straight forward and the constructor takes hours, minutes and seconds.

this.countdown = new Countdown(2,45,0);

Starting and resuming the countdown timer is done with the setInterval() function and calling the count() method of the countdown object. Pausing the countdown can be done using the clearInterval() function.

The display values for the countdown timer can get obtained by calling the getHours(), getMinutes() and getSeconds() functions of the countdown object.

To determine if the countdown timer is complete call the getSecondsRemaining() function and check to see if the value is -1. If the value is -1 then the countdown timer has completed.

Some samples of implementation are below and a complete sample is available in the GitHub repo.

Interval Function Sample:

function startCountdown()
{
    this.countdownInterval = setInterval(intervalCountdown, 1000);
}
    
function pauseCountdown()
{
    clearInterval(this.countdownInterval);
}

Countdown Interval Sample:

function intervalCountdown()
{
    this.countdown.count();
    this.countdownDiv.innerHTML = this.countdown.getHours() + ":" + this.countdown.getMinutes() + ":" + this.countdown.getSeconds();
        
    if (this.countdown.getSecondsRemaining() == -1)
    {
        clearInterval(this.countdownInterval);
    }
}

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images