I have this error in javascript code : (JavaScript error (Uncaught SyntaxError: Unexpected end of input)) I saw many questions same as this, but could not solve my error, so now ask help My code is so simple as follows;
const playBtn = document.querySelector('.play')
const audio = new Audio('/sounds/1M.mp4')
function playAudio(){
for (i = 0; i < 5; i++){
audio.play();
audio.loop = true;
audio.playbackRate = 2;
}
playBtn.addEventListener('click', ()=> {
playAudio()
})
Why this kind of error comes out from my code? Uncaught SyntaxError: Unexpected end of input) : audio.js 14 (line 14 is the empty line next the end of parenthesis '})' How the empty line gives an error ?
You didn't close the for loop
const playBtn = document.querySelector('.play')
const audio = new Audio('/sounds/1M.mp4')
function playAudio(){
for (i = 0; i < 5; i++){
audio.play();
audio.loop = true;
audio.playbackRate = 2;
}
}
playBtn.addEventListener('click', ()=> {
playAudio()
})
It is because you forgot to close the playAudio function when defining it. You can fix it like this:
const playBtn = document.querySelector('.play')
const audio = new Audio('/sounds/1M.mp4')
function playAudio() {
for (i = 0; i < 5; i++) {
audio.play();
audio.loop = true;
audio.playbackRate = 2;
} // <---
}
playBtn.addEventListener('click', () => {
playAudio();
});