How do I get the audio to stop after x number of time

how do i get the the audio to stop after a 7 sec
12 Replies
Jochem
Jochem2y ago
you have to keep the audio variable in the main scope, and then call audio.stop() in the timeout callback
kirin-808
kirin-8082y ago
I think it's audio.pause() though, not sure audio.stop() exists in the API
vert_Nk26☑
vert_Nk26☑OP2y ago
Audio. Stop is not working
kirin-808
kirin-8082y ago
Tried with pause() ?
Jochem
Jochem2y ago
Kirin is right, it's audio.pause() if that doesn't work, paste your code in a code block (not a screenshot please), and we can take another look 🙂
vert_Nk26☑
vert_Nk26☑OP2y ago
Ok am going to try with pause // let audio = new Audio("bell.mp3") // if (minutes == 5 && seconds == 0) { document.querySelector("#hiddenmessage")?.classList.add("show") playmusic() setTimeout( () =>
document.querySelector("#hiddenmessage")?.classList.remove("show"), 7000 )
Jochem
Jochem2y ago
if you start that with ```js and end it with ```, it'll format properly and add syntax highlighting
vert_Nk26☑
vert_Nk26☑OP2y ago
//
let audio = new Audio("bell.mp3")

//

if (minutes == 5 && seconds == 0) {
document.querySelector("#hiddenmessage")?.classList.add("show")
playmusic()

setTimeout(
() =>

document.querySelector("#hiddenmessage")?.classList.remove("show"),
7000
)
//
let audio = new Audio("bell.mp3")

//

if (minutes == 5 && seconds == 0) {
document.querySelector("#hiddenmessage")?.classList.add("show")
playmusic()

setTimeout(
() =>

document.querySelector("#hiddenmessage")?.classList.remove("show"),
7000
)
Jochem
Jochem2y ago
where is playmusic defined?
vert_Nk26☑
vert_Nk26☑OP2y ago
xd giv me a sec did copy that

let audio = new Audio("bell.mp3")
const playAudio = () =>{
audio.play;
}
//

if (minutes == 5 && seconds == 0) {
document.querySelector("#hiddenmessage")?.classList.add("show")
playAudio()

setTimeout(
() =>

document.querySelector("#hiddenmessage")?.classList.remove("show"),
7000
)

}

let audio = new Audio("bell.mp3")
const playAudio = () =>{
audio.play;
}
//

if (minutes == 5 && seconds == 0) {
document.querySelector("#hiddenmessage")?.classList.add("show")
playAudio()

setTimeout(
() =>

document.querySelector("#hiddenmessage")?.classList.remove("show"),
7000
)

}
Jochem
Jochem2y ago
setTimeout(() => {
audio.pause();
document.querySelector("#hiddenmessage")?.classList.remove("show")
}, 7000);
setTimeout(() => {
audio.pause();
document.querySelector("#hiddenmessage")?.classList.remove("show")
}, 7000);
replace your setTimeout with that
vert_Nk26☑
vert_Nk26☑OP2y ago
@jochemm is not working but thanks anyways I think I need some rest @jochemm is working now but I don't understand why is working xd I ended up using || and is working
Want results from more Discord servers?
Add your server