Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

ontimeupdate — handling media time updates

The ontimeupdate attribute contains JavaScript that handles the timeupdate event, used when an audio or video playback position is updated. It can refresh a time indicator but does not provide precise timing.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<audio ontimeupdate="JavaScript statements"></audio>
<video ontimeupdate="JavaScript statements"></video>

ontimeupdate is a global HTML event handler attribute for timeupdate. Its value is a JavaScript function body, not a URL or merely a function declaration. It is normally used on audio and video elements; its availability on other elements does not mean they automatically emit this event.

Inside the handler, this refers to the element, and this.currentTime provides the playback position in seconds. The browser emits timeupdate during playback and in other situations involving position changes, such as seeking. Its frequency is not fixed: the event is neither a clock nor a notification for every frame.

The HTML attribute ontimeupdate contains source text, whereas the JavaScript property element.ontimeupdate accepts a function or null. An alternative is element.addEventListener('timeupdate', handler). Content Security Policy can block code in attributes.

The example deliberately dispatches a synthetic event without a media file. It demonstrates handling the event and reading the initial currentTime, not actual playback or seeking. The expected text is 0.0 s.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>ontimeupdate demo</title>
<audio id="player" ontimeupdate="document.getElementById('result').textContent = this.currentTime.toFixed(1) + ' s';"></audio>
<output id="result">Waiting</output>
<script>
  const player = document.getElementById('player');
  player.dispatchEvent(new Event('timeupdate'));
</script>
</html>

Result

0.0 s

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources