Reputation: 21
I´m new to html js and css and my objective is do anything similar to spotify or any app that plays music and my objective is other audio plays when one of them stops. I tried this, but it doesnt work when i load the page the audio is already on 10 seconds stated on the "if" and the alert pops up... probably this is very easy but, you know just learning.
if (audio.currentTime = 10) {
alert("Hello! I am an alert box!!");
}
Upvotes: 2
Views: 110
Reputation: 308
You can try this:
var audio = document.getElementById('audio');
var timed10 = false;
audio.addEventListener('timeupdate', (time) => {
if(time>=10 && !timed10){
timed10 = true;
// you can do any thing just here for example alert('hi')
}
});
Upvotes: 2
Reputation: 21
for someone that need help' for this i done this:
var audio = document.getElementById('audio');
var play = document.getElementById('play');
var count = 0;
> function playaud(){ if(count == 0){
> count = 1;
> audio.play();
> audio2.load();
> audio3.load(); // stop other musics that could be playing before
> audio4.load();
> audio5.load(); }else{
> count = 0;
> audio.pause();
> audio.load(); // restart the music use audio.pause() to pause i instead of stop it
> count2 = 0;
> count3 = 0; //this to put the count at 0 becouse i have 5 musics and if they were playing before this one the count would stay at 1 and
> that cause a bug
> count4 = 0;
> count5 = 0;
>
>
> } }
<button onclick="playaud()" id="play" type="button" name="button">Play ►</button>
Upvotes: 0
Reputation: 106
There's an error in your if statement, you're setting the value of audio.currentTime
instead of checking its value, in JS you can use == to check if the value matches, independent of the type and === that will match the type too, so, if you have:
10 == "10" // true but 10 === "10" // false because the "10" is a string and not a number.
I'd recommend you to take a look at MDN about the if statement in JS.
Upvotes: -1