forked from codemistic/Web-Development
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
71 lines (52 loc) · 1.92 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
var countInterval;
function startCounter() {
var number = parseInt(document.getElementById("number").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 > 99999) {
alert("Range out of bounds");
clearInterval(countInterval);
return;
}
var currentNos = document.querySelectorAll(".counter .current");
var nextNos = document.querySelectorAll(".counter .next");
var count = 0;
// If user clicks on 'Start Counter' button again - remove this function and below line if you don't consider this situation
resetNumbers(currentNos, nextNos, 5);
// Clears the previous interval that was running
clearInterval(countInterval);
countInterval = setInterval(function() {
if(count === number) {
clearInterval(countInterval);
alert("Counter has stopped");
return;
}
increaseCount(currentNos, nextNos, 4);
count++;
}, 1000);
}
function resetNumbers(currentNos, nextNos, end) {
for(var i=0; i<end; ++i) {
currentNos[i].innerText = 0;
nextNos[i].innerText = 1;
}
}
function increaseCount(currentNos, nextNos, index) {
let current = currentNos[index];
let next = nextNos[index];
if(current.innerText == 9) {
increaseCount(currentNos, nextNos, 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);
}