Skip to content
Advertisement

javascript: How to trigger an if statement when a variable equals a number in an array? [closed]

Basically what I have is an array with these numbers: var songTimer = [0,12,49]. What I want to happen is that every time audio.currentTime(a built in javascript variable for audio) reaches any of the numbers in songTimer’s array, it will trigger an if statement (say, post a message in the console). I haven’t written any code for this yet, so yeah.

Advertisement

Answer

var songTimer = [0,12,49];
audio.addEventListener("timeupdate", function(){
  if (songTimer.includes(audio.currentTime)) {
    // do something
  }
}); 
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement