So, I have this very simple Vanilla JS + HTML
code wherein I can input my desired number of seconds and it will countdown on button
click. I need help improving this code wherein, instead of counting down the inputted seconds, it will convert the seconds to hours, minutes, and seconds on button
click and it will countdown the result at the same time. Think of it as a seconds to HH:MM:SS converter + Countdown.
Note: I want to run this code offline without dependencies. So, I will greatly appreciate it if we can come up with a solution using Vanilla JS. As simple as this may be for most, my knowhow in JS programming is really mediocre. Sorry. Thanks for the help and suggestions in advance.
HTML
<form>
<p>Input Seconds:</p>
<input id="number" type="text" autocomplete="off"/>
<button type="button" id="start">Start Timer</button>
</form>
<div id="area"><p id="text1" style="color: red;">Click Start</p></div>
Vanilla Javascript
<script>
window.onload = init;
function init(){
var start = document.getElementById("start");
start.onclick = finalCountDown;
}
function finalCountDown() {setTimeout(countDown(),1000);}
function countDown(){
var number = document.getElementById("number");
var value = parseInt(number.value, 10);
var area = document.getElementById("text1");
var audio = new Audio('audioURL.mp3');
var interval = setInterval(function() {
if (value > 1) {
area.innerText = value-1;
value--;
} else {
clearInterval(interval);
area.innerText = "Countdown Ended";
audio.play()
}
}, 1000);
}
</script>