Maybe the native timeupdate
event is what you're looking for (see the docs on MDN):
document.addEventListener('timeupdate', function(e) {
var audioElement = e.target;
// do something
});
According to the docs:
The timeupdate
event is fired when the time indicated by the currentTime attribute has been updated.
Edit
As pointed out in the comments, attaching the event to the document will listen to all timeupdate
events originating from all audio elements on the page. However, this is out of scope of the question, so the business logic inside of the event handler is left up to its specific implementation.
One benefit of attaching to the document is that you automatically support dynamically generated audio elements without having to re-attach stale event listeners. However, it can open up a can of worms when checking which target (supplied by e.target
) fired the event, and what you want to do or not do with that element.