👀 Live Preview
Play the video — elapsed time updates via timeupdate:
Current time: 0.0s

The ontimeupdate attribute is an inline event handler that runs JavaScript when the timeupdate event fires on an audio or video element. It fires as the playback position (currentTime) changes — typically several times per second while media plays. Use it to update a time readout, progress bar, or chapter marker. Read element.currentTime and element.duration inside the handler. Prefer addEventListener("timeupdate", …) in production and keep handler work lightweight.
Media event.
Elements.
Read position.
Bar + clock.
JS property.
Keep it light.
ontimeupdate AttributeThe primary purpose of ontimeupdate is to react whenever the media element’s playback clock advances. That makes it the standard hook for custom players: showing elapsed time, moving a seek bar, highlighting transcript lines, or triggering timed annotations.
Unlike onplay or onpause (which fire once per state change), timeupdate fires repeatedly during playback. Plan your handler accordingly — avoid expensive DOM work on every tick.
Inline ontimeupdate="…" is fine for tutorials. Real apps use video.addEventListener("timeupdate", updateProgress) for cleaner separation and multiple listeners.
Assign a JavaScript function call or expression to ontimeupdate on audio or video:
<video id="player" controls ontimeupdate="updateTime()">
<source src="/video/count.mp4" type="video/mp4">
</video>
<p id="timeDisplay">0:00</p>
<script>
function updateTime() {
const v = document.getElementById("player");
document.getElementById("timeDisplay").textContent =
v.currentTime.toFixed(1) + "s";
}
</script><audio> and <video> elements.timeupdate event fires.timeupdate event (no on prefix in event name).mediaElement.ontimeupdate = function() { … }.currentTime changes — not when paused.currentTime (seconds) and duration for UI math.ontimeupdate accepts a JavaScript handler — not a fixed keyword list:
ontimeupdate="updateTime()" — Call a named function.ontimeupdate="this.currentTime" — Inline expression (rare; prefer functions).element.ontimeupdate = fn — Assign a function object in JavaScript.addEventListener("timeupdate", fn) — Modern recommended approach.<!-- Inline handler -->
<video ontimeupdate="tick()" controls></video>
<!-- Property assignment -->
<script>
video.ontimeupdate = () => console.log(video.currentTime);
</script>
<!-- addEventListener (preferred) -->
<script>
video.addEventListener("timeupdate", tick);
</script>While the media plays, the browser updates currentTime and dispatches timeupdate. Your handler runs each time — use it to refresh UI tied to playback position.
| Item | Details | Notes |
|---|---|---|
| Event name | timeupdate | Handler attr: ontimeupdate |
| Elements | audio, video | HTML5 media only |
| Key property | currentTime | Seconds elapsed |
| Duration | duration | Use ondurationchange for metadata |
| Fire rate | ~4×/sec (varies) | Not every animation frame |
| Modern API | addEventListener | Preferred over inline |
| Element | Supports ontimeupdate? | Notes |
|---|---|---|
<video> | Yes | Most common use case |
<audio> | Yes | Podcast players, music UI |
<source> | No | Put handler on parent media element |
<track> | No | Listen on video/audio instead |
<input> | No | Not a media time event |
ontimeupdate vs related media events| Event / attribute | When it fires | Typical use |
|---|---|---|
ontimeupdate | currentTime changes during play | Progress bar, elapsed clock |
ondurationchange | duration metadata available | Total length label |
onseeked | User/script seek completes | Jump to chapter |
onprogress | Buffered ranges change | Buffer indicator |
onplay / onpause | Playback starts / stops | Play button state |
Inline ontimeupdate time display, dynamic element.ontimeupdate, and a progress bar with addEventListener.
Play the video — elapsed time updates via timeupdate:
Current time: 0.0s
ontimeupdateShow current playback seconds with a named function:
<video id="myVideo" controls ontimeupdate="updateTime()">
<source src="/video/count.mp4" type="video/mp4">
</video>
<p id="timeDisplay">Current time: 0.00s</p>
<script>
function updateTime() {
const video = document.getElementById("myVideo");
document.getElementById("timeDisplay").textContent =
"Current time: " + video.currentTime.toFixed(2) + "s";
}
</script>Each timeupdate event calls updateTime(), which reads currentTime and refreshes the display.
Assign ontimeupdate programmatically:
<video id="dynamicVideo" controls>
<source src="/video/count.mp4" type="video/mp4">
</video>
<script>
const video = document.getElementById("dynamicVideo");
const display = document.getElementById("timeDisplay");
video.ontimeupdate = function() {
display.textContent = "Current time: " + video.currentTime.toFixed(2) + "s";
};
</script>Setting the property replaces any previous ontimeupdate handler. Use addEventListener if you need multiple listeners.
addEventListenerUpdate a range input as playback advances (modern pattern):
<video id="player" controls></video>
<input type="range" id="progress" min="0" max="100" value="0" aria-label="Playback progress">
<script>
const player = document.getElementById("player");
const bar = document.getElementById("progress");
player.addEventListener("timeupdate", () => {
if (!player.duration) return;
bar.value = (player.currentTime / player.duration) * 100;
});
</script>addEventListener("timeupdate") is the production-friendly way to drive custom progress UI.
aria-label or visible labels.controls on video/audio provides accessible playback UI out of the box.<track kind="captions"> for accessible media.Browser advances currentTime.
Several times per second.
Read currentTime, update UI.
Clock and seek bar stay in sync.
The timeupdate event and ontimeupdate handler are supported in all modern browsers on HTML5 audio and video.
All major browsers fire timeupdate during media playback.
timeupdate ExcellentBottom line: Safe for custom media players in all modern browsers.
timeupdate for progress bars and elapsed time labelsaddEventListener("timeupdate", …) in production codeduration is valid before dividing for percentagesondurationchange for total length displaytimeupdate fires every frame — it does notcontrols or custom accessible player UItimeupdate with progress (buffering)The ontimeupdate attribute connects the timeupdate event to your JavaScript — the standard way to keep custom media UI in sync with playback position.
Start with inline handlers for learning, then move to addEventListener for real players. Read currentTime, keep handlers lightweight, and combine with other media events for a complete experience.
ontimeupdateBookmark these when building media players.
Media event.
CoreElements.
ScopeSeconds.
PropertyPreferred.
ModernFast handlers.
Perftimeupdate event fires — as currentTime changes during audio or video playback.<audio> and <video>. Attach the handler to the media element itself.ontimeupdate tracks playback position. ondurationchange fires when total duration metadata is known or changes.addEventListener("timeupdate", handler) in production. Inline ontimeupdate is fine for tutorials and quick demos.Practice ontimeupdate with live time displays and progress bars in the Try It editor.
3 people found this page helpful