Random clock counting

I find my self in a situation where I need to display many clocks in this format: HH:MM:SS, which isn’t a problem, but I need loads of these, all starting at a pre-defined times and still simultaneously counting up as a clock would. e.g. 05:52:13 | 03:14:32 | 07:41:11 … and so on. Here’s my basic clock script, but I have no idea where to go from here. I’m still a javascript noob

function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('txt').innerHTML =
    h + ":" + m + ":" + s;
    var t = setTimeout(startTime, 500);
}
function checkTime(i) {
    if (i < 10) {i = "0" + i};
    return i;
}

Any help would be much appreciated :slight_smile:

JavaScript stores clock times in terms of the number of seconds from Jan 1st 1970.

I recommend that you use that number as a base for which each clock then just adds an offset, to get the desired time/date that you require.

Also since you need many clocks that all function the same way, look into using Classes. Create a clock class so you can instantiate as many as needed all with different attributes (such as the time offset).

Then each instance of a clock class can be independently controlled. And the class can have methods for anything you need; starting, stopping, resetting, whatever.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.