Complete JavaScript beginner here. I'm coding a timer that reset's its countdown whenever the mouse moves or a key on the keyboard is pressed. With a little luck I was able to get it to function, but I don't know how to get the audio to my alarm.mp3 file to play.
I know it's probably simple, but after almost a month of figuring this out I'm really getting burned out. Here's my JavaScript, any help is 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');
}
I'm using Visual Studio Code on Windows 11.
// You can create a sound source as follows:
const audio = new Audio("/path/to/you/alarm.mp3");
// And when you need to play a sound, all you have to do is call the following function:
audio.play();