'Cannot access "audio" before initialization in js problem at line 2
Made a clock and alarm a while back and was going to maybe add some more and now the clock is no longer displaying. Problem possibly around line 2 (Learning to code I know its probably something simple) The error code I get back is just the initialization of the audio saying that it is unable to access even though it's in the same file
'''
const display = document.getElementById('clock');
const audio = new audio('finAlarm.wav');
audio.loop = true;
let alarmTime = null;
let alarmTimeout = null;
function updateTime() {
const date = new Date();
const hour = formatTime(date.getHours());
const minutes = formatTime(date.getMinutes());
const seconds = formatTime(date.getSeconds());
display.innerText=`${hour} : ${minutes} : ${seconds}`
}
function formatTime(time) {
if ( time < 10 ) {
return '0' + time;
}
return time;
}
function setAlarmTime(value) {
alarmTime = value;
}
function setAlarm() {
if(alarmTime) {
const current = new Date();
const timeToAlarm = new Date(alarmTime);
if (timeToAlarm > current) {
const timeout = timeToAlarm.getTime() - current.getTime();
alarmTimeout = setTimeout(() => audio.play(), timeout);
alert('Alarm set');
}
}
}
function clearAlarm() {
var name = prompt("are you up?>:)")
if (name ==null || name=="") {
txt = "incorrect >:(|)"
} else {
audio.pause();
audio.currentTime = 0;
alert('Alarm Stopped')
}
alert(txt);
audio.pause();
audio.currentTime = 0;
if (alarmTimeout) {
clearTimeout(alarmTimeout);
alert('Alarm Stopped');
}
}
setInterval(updateTime, 1000);
'''
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
