I had written the following code:
var el = document.getElementsByClassName("header")[0];
function animate() {
c = 0;
I = setInterval(adapt,100);
function adapt() {
if (c == 255) {
clearInterval(I);
}
else {
el.style.backgroundColor = "rgb(" + 0 + "," + 0 + "," + c + ")";
c++;
}
}
}
el.addEventListener("click",animate);
If I clicked the header the colour changed gradually from black to blue in a little over 25500 milliseconds as was to be expected, but if you clicked the header again before the animation had fully elapsed strange things would happen. The rate at which the colour changed increased permanently, so if the header was clicked again at any point after that it would take significantly less than 25500 milliseconds for the colour to change completely. It seemed like the animation played faster and faster as you would click more often and in quicker succession.
I have come to learn that I am not as talented as I thought I was and that I simply should have put
let c = 0;
let I = setInterval(adapt,100);
and this indeed fixes the strange behaviour. However I still wonder why this behaviour occurred. All I could find online was that omitting keywords creates global variables and will cause strange errors in general, but not how such errors can occur.