0

Here is what i have:

<audio id='audio' src="lol.mp3" autoplay></audio>

and:

var currentTime = document.getElementById('audio').currentTime
var timePoint = 26.5

function lolz() {
document.write("lolz")
}

What i want:

I want to run the function lolz() when currentTime is greater than timePoint.

gyre
  • 16,369
  • 3
  • 37
  • 47
froggyman
  • 105
  • 2
  • You'll need an event handler, for instance something that triggers when the audio plays. Start searching MDN – adeneo Mar 26 '17 at 21:42
  • See http://stackoverflow.com/questions/38768375/html5-audio-streaming-precisely-measure-latency/38842623#38842623 – guest271314 Mar 26 '17 at 22:02

1 Answers1

3

Your should make your lolz function an event listener by attaching it to the timeupdate event.


Your Updated Code:

document.getElementById('audio').addEventListener('timeupdate', lolz)
var timePoint = 26.5

function lolz () {
  if (this.currentTime > timePoint) {
    document.body.textContent = "lolz"
  }
}

<audio id="audio" src="lol.mp3" autoplay></audio>


Working Example Snippet:

document.getElementById('audio').addEventListener('timeupdate', lolz)
var timePoint = 3.1

function lolz () {
  if (this.currentTime > timePoint) {
    document.body.textContent = "lolz"
  }
}
<audio id="audio" src="//mdn.mozillademos.org/files/2587/AudioTest.ogg" autoplay></audio>
gyre
  • 16,369
  • 3
  • 37
  • 47