Click here to Skip to main content
15,886,110 members
Articles / Web Development / XHTML

JavaScript Stopwatch

Rate me:
Please Sign up or sign in to vote.
4.17/5 (8 votes)
14 Sep 2008CPOL2 min read 118.8K   2.9K   17  
A simple stopwatch object using JavaScript.
function StopWatch(){

var startTime = null;
var stopTime = null;
var running = false;

this.start = function(){
    
    if (running == true)
        return;
    else if (startTime != null)
        stopTime = null;
    
    running = true;    
    startTime = getTime();
}

this.stop = function(){
    
    if (running == false)
        return;    
    
    stopTime = getTime();
    running = false;
}

this.duration = function(){
    if (startTime == null || stopTime == null)
        return 'Undefined';
    else
        return (stopTime - startTime) / 1000;
}

this.isRunning = function() { return running; }

function getTime(){
    var day = new Date();
    return day.getTime();
}


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
Ah, just another dev living the life of Dilbert. Smile | :)

Comments and Discussions