Start/Stop Hour register

by NicosKaralis

HTML

<a href="#" id="stop">Stop</a><br />
<div id="time"></div>

JavaScript

/* Start/Stop Time counter */
var t;
var timer_is_on = 0;

function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

function startTime(id) {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    // add a zero in front of numbers<10
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById(id).innerHTML = h + ":" + m + ":" + s;
    t = setTimeout('startTime(\'' + id + '\')', 500);
}


function doTimer(id) {
    if (!timer_is_on) {
        timer_is_on = 1;
        startTime(id);
    }
}

function stopCount() {
    clearTimeout(t);
    timer_is_on = 0;
}

$(function() {
    doTimer('time');
});

$("a#stop").click(function() {
    if ($(this).text() == "Start") {
        doTimer('time');
        $(this).text("Stop");
    } else {
        stopCount();
        $(this).text("Start");
    }
    return false;
});