Hi everyone, I'm a complete beginner in JavaScript. Recently, I've been working on coding a timer that resets its countdown whenever there is mouse movement or a key on the keyboard is pressed. I've managed to get the timer functionality working, but I'm struggling to figure out how to play the audio from my alarm.mp3 file.
I know it's a simple issue, but after spending almost a month trying to solve it, I'm starting to feel burnt out. Below is my JavaScript code, and any help or guidance would be greatly appreciated.
const fiveMinutes = 5
var start, diff, minutes,seconds;
var display;
var timerStarted = false
var resetCtr = 0;
var debugMsg;
const debug = true;
function startTimer(duration) {
start = Date.now()
function timer() {
diff = duration - (((Date.now() - start) / 1000) | 0);
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
};
timer();
setInterval(timer, 1000);
}
function btnStartTimer() {
if (!timerStarted) {
timerStarted = true;
startTimer(fiveMinutes);
}
}
function resetTimer(){
if (timerStarted) {
startTimer(fiveMinutes);
}
}
window.onload = function () {
display = document.querySelector('#time');
}
Just to give you a bit of context, I'm using Visual Studio Code on Windows 11 for this project.