I have some problems with my timer. When changing the timer duration, I can't make the timer start from the new duration. How can I fix that? Please help. Here is my code:
CSS:
div {
float: left;
width: 100px;
height: 100px;
border: 1px solid;
text-align: center;
}
HTML:
<div class='session'>2</div>
<div id='increase' onclick='decrease()'>-</div>
<div id='increase' onclick='increase()'>+</div>
<div class='session' id='session' onclick='setInterval(changeSessionDuration,100)'>2</div>
JS:
var x = document.getElementsByClassName('session');
var seconds = 60;
function increase() {
for (var i = 0; i < x.length; i++) {
x[i].innerHTML++;
}
}
function decrease() {
for (var i = 0; i < x.length; i++) {
if (x[i].innerHTML > 0) {
x[i].innerHTML--;
}
}
}
var session = x[1].innerHTML - 1;
function changeSessionDuration() {
if (seconds > 0) {
seconds--;
if (seconds == 0 && session > 0) {
session--;
seconds = 60;
}
}
x[1].innerHTML = session + ':' + seconds;
}