()
| 1 | var countInterval; |
| 2 | |
| 3 | function startCounter() { |
| 4 | |
| 5 | var number = parseInt(document.getElementById("number").value); |
| 6 | |
| 7 | if (isNaN(number)) { |
| 8 | alert("Please enter a number"); |
| 9 | clearInterval(countInterval); // This is important for the condition when a counter is running and you entered a wrong input for a new counter |
| 10 | return; |
| 11 | } |
| 12 | if (number < 1 || number > 99999) { |
| 13 | alert("Range out of bounds"); |
| 14 | clearInterval(countInterval); |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | var currentNos = document.querySelectorAll(".counter .current"); |
| 19 | var nextNos = document.querySelectorAll(".counter .next"); |
| 20 | var count = 0; |
| 21 | |
| 22 | // If user clicks on 'Start Counter' button again - remove this function and below line if you don't consider this situation |
| 23 | resetNumbers(currentNos, nextNos, 5); |
| 24 | |
| 25 | // Clears the previous interval that was running |
| 26 | clearInterval(countInterval); |
| 27 | |
| 28 | countInterval = setInterval(function () { |
| 29 | if (count === number) { |
| 30 | clearInterval(countInterval); |
| 31 | alert("Counter has stopped"); |
| 32 | return; |
| 33 | } |
| 34 | increaseCount(currentNos, nextNos, 4); |
| 35 | count++; |
| 36 | }, 1000); |
| 37 | |
| 38 | } |
| 39 | |
| 40 | |
| 41 | function resetNumbers(currentNos, nextNos, end) { |
nothing calls this directly
no test coverage detected