---
title: 'HTMLMediaElement: durationchange 事件'
slug: Web/API/HTMLMediaElement/durationchange_event
tags:
  - Audio
  - Event
  - HTMLMe
  - HTMLMediaElement
  - Reference
  - Video
translation_of: Web/API/HTMLMediaElement/durationchange_event
---
<p>{{APIRef("HTMLMediaElement")}}</p>

<p><span class="seoSummary"><code>durationchange</code> 事件会在 <code>duration</code> 属性更新时被触发。</span></p>

<table class="properties">
 <tbody>
  <tr>
   <th scope="row">Bubbles</th>
   <td>No</td>
  </tr>
  <tr>
   <th scope="row">Cancelable</th>
   <td>No</td>
  </tr>
  <tr>
   <th scope="row">Interface</th>
   <td>{{DOMxRef("Event")}}</td>
  </tr>
  <tr>
   <th scope="row">Target</th>
   <td>Element</td>
  </tr>
  <tr>
   <th scope="row">Default Action</th>
   <td>None</td>
  </tr>
  <tr>
   <th scope="row">Event handler property</th>
   <td>{{domxref("GlobalEventHandlers.ondurationchange")}}</td>
  </tr>
  <tr>
   <th scope="row">Specification</th>
   <td><a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#event-media-playing">HTML5 media</a></td>
  </tr>
 </tbody>
</table>

<h2 id="例子">例子</h2>

<p>下面的例子为 HTMLMediaElement 的 <code>durationchange</code>  事件添加事件监听器,然后在事件触发时发送一个消息。</p>

<p>使用 <code>addEventListener()</code>:</p>

<pre class="brush: js notranslate">const video = document.querySelector('video');

video.addEventListener('durationchange', (event) =&gt; {
  console.log('Not sure why, but the duration of the video has changed.');
});</pre>

<p>使用 <code>ondurationchange</code> 事件处理器属性:</p>

<pre class="brush: js notranslate">const video = document.querySelector('video');

video.ondurationchange = (event) =&gt; {
  console.log('Not sure why, but the duration of the video has changed.');
};</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', "media.html#event-media-durationchange", "durationchange media event")}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
  </tr>
  <tr>
   <td>{{SpecName('HTML5 W3C', "embedded-content-0.html#event-media-durationchange", "durationchange media event")}}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("api.HTMLMediaElement.durationchange_event")}}</p>

<h2 id="Related_Events">Related Events</h2>

<ul>
 <li>{{domxref("HTMLMediaElement.playing_event", 'HTMLMediaElement: playing event')}}</li>
 <li>{{domxref("HTMLMediaElement.waiting_event", 'HTMLMediaElement: waiting event')}}</li>
 <li>{{domxref("HTMLMediaElement.seeking_event", 'HTMLMediaElement: seeking event')}}</li>
 <li>{{domxref("HTMLMediaElement.seeked_event", 'HTMLMediaElement: seeked event')}}</li>
 <li>{{domxref("HTMLMediaElement.ended_event", 'HTMLMediaElement: ended event')}}</li>
 <li>{{domxref("HTMLMediaElement.loadedmetadata_event", 'HTMLMediaElement: loadedmetadata event')}}</li>
 <li>{{domxref("HTMLMediaElement.loadeddata_event", 'HTMLMediaElement: loadeddata event')}}</li>
 <li>{{domxref("HTMLMediaElement.canplay_event", 'HTMLMediaElement: canplay event')}}</li>
 <li>{{domxref("HTMLMediaElement.canplaythrough_event", 'HTMLMediaElement: canplaythrough event')}}</li>
 <li>{{domxref("HTMLMediaElement.durationchange_event", 'HTMLMediaElement: durationchange event')}}</li>
 <li>{{domxref("HTMLMediaElement.timeupdate_event", 'HTMLMediaElement: timeupdate event')}}</li>
 <li>{{domxref("HTMLMediaElement.play_event", 'HTMLMediaElement: play event')}}</li>
 <li>{{domxref("HTMLMediaElement.pause_event", 'HTMLMediaElement: pause event')}}</li>
 <li>{{domxref("HTMLMediaElement.ratechange_event", 'HTMLMediaElement: ratechange event')}}</li>
 <li>{{domxref("HTMLMediaElement.volumechange_event", 'HTMLMediaElement: volumechange event')}}</li>
 <li>{{domxref("HTMLMediaElement.suspend_event", 'HTMLMediaElement: suspend event')}}</li>
 <li>{{domxref("HTMLMediaElement.emptied_event", 'HTMLMediaElement: emptied event')}}</li>
 <li>{{domxref("HTMLMediaElement.stalled_event", 'HTMLMediaElement: stalled event')}}</li>
</ul>

<h2 id="See_Also">See Also</h2>

<ul>
 <li>{{domxref("HTMLAudioElement")}}</li>
 <li>{{domxref("HTMLVideoElement")}}</li>
 <li>{{HTMLElement("audio")}}</li>
 <li>{{HTMLElement("video")}}</li>
</ul>