What’s Javascript ?

Ans. Javascript is a simple programming language built into Netscape 2.0 and higher. It’s built-in with and embedded in HTML. It permits better management of internet web page habits than does HTML alone. Because the Javascript interpreter is a part of Netscape, it’s platform-independent: Javascript integrated into HTML runs on Home windows, Macintosh, and different Netscape-supported methods.

Though Javascript resembles Java, it’s not the identical as Java. Java is a extra in depth and highly effective, platform-independent programming language, usually used to create applet applications which run in Netscape, but additionally able to creating stand-alone applications. Javascript provides HTML authors management over Java applet applications. Java is compiled on the server, and the item code is handed to the client for execution; Javascript is interpreted by the client.

What’s Stop Watch?

A stopwatch is a handheld timepiece designed to measure the amount of time elapsed from a particular time when it is activated to the time when the piece is deactivated.

Let’s see how we can make online stop watch by using javascript:

Make HTML like this which is mentioned below you can copy from here. Put it after <body> tag.

<h1><time>00:00:00</time></h1>
<button id="start">start</button>
<button id="stop">stop</button>
<button id="clear">clear</button>

After HTML you need to write Javascript code to make HTML workable. Here is the source code for making online stop watch.

var h1 = document.getElementsByTagName('h1')[0],
    start = document.getElementById('start'),
    stop = document.getElementById('stop'),
    clear = document.getElementById('clear'),
    seconds = 0, minutes = 0, hours = 0,
    t;

function add() {
    seconds++;
    if (seconds >= 60) {
        seconds = 0;
        minutes++;
        if (minutes >= 60) {
            minutes = 0;
            hours++;
        }
    }
    
    h1.textContent = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);

   timer()
}
function timer() {
    t = setTimeout(add, 1000);
}



/* Start button */
start.onclick = function(){
	timer()
};

/* Stop button */
stop.onclick = function() {
    clearTimeout(t);
	
}

/* Clear button */
clear.onclick = function() {
    h1.textContent = "00:00:00";
    seconds = 0; minutes = 0; hours = 0;
	timer()
}

That’s it. See how it is easy to make an online stop watch. You can modify code if you want online stop watch don’t run after clear button just remove timer() function from the clear button action.

Output of Online Stop Watch:

About The Author

Related Posts

Leave a Reply

Your email address will not be published.