-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (52 loc) · 1.81 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var countInterval;
function startCounter() {
var number = parseInt(document.getElementById("input").value);
if (isNaN(number)) {
alert("Please enter a number");
clearInterval(countInterval); // This is important for the condition when a counter is running and you entered a wrong input for a new counter
return;
}
if (number < 1 || number > 9999) {
alert("Range out of bounds");
clearInterval(countInterval);
return;
}
var currentNo = document.querySelectorAll(".output .numbers");
var nextNo = document.querySelectorAll(".output .next");
var count = 0;
// If user clicks on 'Start Counter' button again - remove this function and below 2 lines if you don't consider this situation
resetNumbers(currentNo, nextNo, 4);
// Clears the previous interval that was running
clearInterval(countInterval);
countInterval = setInterval(function () {
if (count === number) {
clearInterval(countInterval);
alert("Counter has stopped");
return;
}
increaseCount(currentNo, nextNo, 3);
count++;
}, 1000);
}
function resetNumbers(currentNo, nextNo, end) {
for (var i = 0; i < end; ++i) {
currentNo[i].innerText = 0;
nextNo[i].innerText = 1;
}
}
function increaseCount(currentNo, nextNo, index) {
let current = currentNo[index];
let next = nextNo[index];
if (current.innerText == 9) {
increaseCount(currentNo, nextNo, index - 1);
}
next.classList.add("animate");
setTimeout(function () {
current.innerText = next.innerText;
next.classList.remove("animate");
next.innerText = parseInt(next.innerText) + 1;
if(next.innerText > 9) {
next.innerText = 0;
}
}, 500);
}