Home > OS >  javascript: How to trigger an if statement when a variable equals a number in an array?
javascript: How to trigger an if statement when a variable equals a number in an array?

Time:08-25

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.

CodePudding user response:

I think you want something like:

var songTimer = [0,12,49];

if (songTimer.includes(audio.currentTime)) {
  // do something
}

CodePudding user response:

var songTimer = [0,12,49];
audio.addEventListener("timeupdate", function(){
  if (songTimer.includes(audio.currentTime)) {
    // do something
  }
}); 
  • Related