ondurationchange

The ondurationchange event is triggered when the duration of a media file is updated. This typically happens when an audio or video is loaded, changing the duration from "NaN" to a valid value.

ondurationchange event

The ondurationchange event is activated when the duration of a media element (such as <audio> or <video>) is modified. This can happen when the media is loaded or when its metadata is refreshed.

Syntax

In HTML

index.html
<element ondurationchange="myScript">

In JS

script.js
object.ondurationchange = function(){myScript};

In JavaScript, the addEventListener() function is used.

script.js
object.addEventListener("durationchange", myScript);

Example

::event-ondurationchange ::
index.html
<!DOCTYPE html> 
<html> 
<body> 
<h1>HTML DOM Events</h1>
<h2>The ondurationchange Event</h2>

<video controls ondurationchange="myFunction()">
  <source src="mov_bbb.mp4" type="video/mp4">
  <source src="mov_bbb.ogg" type="video/ogg">
  Your browser does not support video playback.
</video>

<p id="demo">Event has fired: </p>

<p>Video courtesy of <a href="https://institute.qarpeo.com" target="_blank">Tree</a>.</p>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML += "Duration has changed";
}
</script>

</body> 
</html>

Value

Conclusion

The ondurationchange event fires when the duration of a media element is updated. It’s commonly used with <audio> and <video> elements to respond to changes in media metadata. This event helps ensure the media is properly handled as its properties change.