Timer


What's that?

You can also start and stop a stopwatch or timer like object using setTimeout(). Here's how:

				

STEP ONE
Make The Button:
< button onclick="timedText()">start counting< /button >
< input type="text" id="txt" >

STEP TWO
Activate The Script:
< script >
var c = 0;
var t;
var timer_is_on = 0;

function timedCount() {
    document.getElementById("txt").value = c;
    c = c + 1;
    t = setTimeout(function(){ timedCount() }, 1000);
}

function startCount() {
    if (!timer_is_on) {
        timer_is_on = 1;
        timedCount();
    }
}

function stopCount() {
    clearTimeout(t);
    timer_is_on = 0;
} < / script >

				
			

 

Code in action!

View at full scale