This is my first JavaScript application (it's inspired by this timer). How could I improve it?
I had to set the setInterval()
to 980ms instead of 1000ms because it was slower than a real clock for some reason.
Main JS:
var btn1 = document.getElementById('start');
var btn2 = document.getElementById('pause');
var btn3 = document.getElementById('restart')
var h1 = document.getElementById('h1');
var h1Clr = h1.style.color;
var h1Nmb = 0;
var h2 = document.getElementById('h2');
var h2Clr = h1.style.color;
var h2Nmb = 0;
var m1 = document.getElementById('m1');
var m1Clr = h1.style.color;
var m1Nmb = 0;
var m2 = document.getElementById('m2');
var m2Clr = h1.style.color;
var m2Nmb = 0;
var s1 = document.getElementById('s1');
var s1Clr = h1.style.color;
var s1Nmb = 0;
var s2 = document.getElementById('s2');
var s2Clr = h1.style.color;
var s2Nmb = 0;
window.addEventListener("keydown", function (e) {
var keycode = String.fromCharCode(e.keyCode);
var isNmbr = Number(keycode);
if (s2Clr == 'white' && (isNmbr || keycode == 0)) {
s2.textContent = String.fromCharCode(e.keyCode);
s2.style.color = 'rgb(192, 33, 33)';
s2Clr = 'rgb(192, 33, 33)';
window.s2Nmb = Number(s2.textContent);
} else if (s1Clr == 'white' && (isNmbr || keycode == 0)) {
s1.textContent = String.fromCharCode(e.keyCode);
s1.style.color = 'rgb(192, 33, 33)';
s1Clr = 'rgb(192, 33, 33)';
window.s1Nmb = Number(s1.textContent);
} else if (m2Clr == 'white' && (isNmbr || keycode == 0)) {
m2.textContent = String.fromCharCode(e.keyCode);
m2.style.color = 'rgb(192, 33, 33)';
m2Clr = 'rgb(192, 33, 33)';
window.m2Nmb = Number(m2.textContent);
} else if (m1Clr == 'white' && (isNmbr || keycode == 0)) {
m1.textContent = String.fromCharCode(e.keyCode);
m1.style.color = 'rgb(192, 33, 33)';
m1Clr = 'rgb(192, 33, 33)';
window.m1Nmb = Number(m1.textContent);
} else if (h2Clr == 'white' && (isNmbr || keycode == 0)) {
h2.textContent = String.fromCharCode(e.keyCode);
h2.style.color = 'rgb(192, 33, 33)';
h2Clr = 'rgb(192, 33, 33)';
window.h2Nmb = Number(h2.textContent);
} else if (h1Clr == 'white' && (isNmbr || keycode == 0)) {
h1.textContent = String.fromCharCode(e.keyCode);
h1.style.color = 'rgb(192, 33, 33)';
h1Clr = 'rgb(192, 33, 33)';
window.h1Nmb = Number(h1.textContent);
}
});
var myVar;
btn1.onclick = function () {
window.myVar = setInterval(myTimer, 980);
}
btn2.onclick = function () {
clearInterval(window.myVar);
}
btn3.onclick = function () {
window.location.reload();
}
function myTimer() {
//s2
if (s2Nmb != 0) {
if (s2Nmb == 0) {
s2Nmb = 10;
};
s2Nmb--;
s2.textContent = s2Nmb;
} else {
s2Nmb = 9;
s2.textContent = s2Nmb;
if (s2Nmb == 0) {
s2Nmb = 10;
};
}
//s1
if (s2Nmb == 9) {
if (s1Nmb == 0) {
s1Nmb = 6;
};
s1Nmb--;
s1.textContent = s1Nmb;
}
//section Minutes
//m2
if (s1Nmb == 5 && s2Nmb == 9) {
if (m2Nmb == 0) {
m2Nmb = 10;
};
m2Nmb--;
m2.textContent = m2Nmb;
}
//m1
if (m2Nmb == 9 && s1Nmb == 5 && s2Nmb == 9) {
if (m1Nmb == 0) {
m1Nmb = 6;
};
m1Nmb--;
m1.textContent = m1Nmb;
}
//h2
if (m1Nmb == 5 && m2Nmb == 9 && s1Nmb == 5 && s2Nmb == 9) {
if (h2Nmb == 0) {
h2Nmb = 10;
};
h2Nmb--;
h2.textContent = h2Nmb;
}
//h1
if (h2Nmb == 9 && m1Nmb == 5 && m2Nmb == 9 && s1Nmb == 5 && s2Nmb == 9) {
if (h1Nmb == 0) {
h1Nmb = 6;
};
h1Nmb--;
h1.textContent = h1Nmb;
}
//alert
if (h1Nmb == 0 && h2Nmb == 0 && m1Nmb == 0 && m2Nmb == 0 && s1Nmb == 0 && s2Nmb == 0) {
clearInterval(window.myVar);
var snd = new Audio("alarm_beep.wav");
snd.volume = 0.3;
snd.play();
}
};