After recently embarking on the journey of developing my very first Chrome extension, I encountered the task of creating a timer that starts as soon as the browser is launched. The timer should increment every second in the format: 00:00:00 --> 00:00:01 --> 00:00:02. Below is the code snippet I have implemented for this functionality. Are there better alternatives out there?
$(function () {
startCount();
});
function startCount() {
timer = setInterval(count, 1000);
}
function count() {
var time_shown = $("#realtime").text();
var time_chunks = time_shown.split(":");
var hour, mins, secs;
hour = Number(time_chunks[0]);
mins = Number(time_chunks[1]);
secs = Number(time_chunks[2]);
secs++;
if (secs == 60) {
secs = 0;
mins = mins + 1;
}
if (mins == 60) {
mins = 0;
hour = hour + 1;
}
if (hour == 13) {
hour = 0;
}
$("#realtime").text(timezero(hour) + ":" + timezero(mins) + ":" + timezero(secs));
}
function timezero(digit) {
var str = digit + '';
if (digit < 10) {
str = "0" + str;
}
return str;
}