HTML ontimeupdate Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

timeupdate

Media event.

02

audio + video

Elements.

03

currentTime

Read position.

04

Progress UI

Bar + clock.

05

.ontimeupdate

JS property.

06

Performance

Keep it light.

Purpose of ontimeupdate Attribute

The 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.

💡
Prefer addEventListener in production

Inline ontimeupdate="…" is fine for tutorials. Real apps use video.addEventListener("timeupdate", updateProgress) for cleaner separation and multiple listeners.

📝 Syntax

Assign a JavaScript function call or expression to ontimeupdate on audio or video:

ontimeupdate.html
<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>

Syntax Rules

  • Valid on <audio> and <video> elements.
  • Value is JavaScript code executed when the timeupdate event fires.
  • Corresponds to the timeupdate event (no on prefix in event name).
  • JavaScript property: mediaElement.ontimeupdate = function() { … }.
  • Fires during playback when currentTime changes — not when paused.
  • Use currentTime (seconds) and duration for UI math.
  • Keep handlers fast; throttle heavy work if needed.

💎 Values

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.
ontimeupdate-values.html
<!-- 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>

How it Works

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.

⚡ Quick Reference

ItemDetailsNotes
Event nametimeupdateHandler attr: ontimeupdate
Elementsaudio, videoHTML5 media only
Key propertycurrentTimeSeconds elapsed
DurationdurationUse ondurationchange for metadata
Fire rate~4×/sec (varies)Not every animation frame
Modern APIaddEventListenerPreferred over inline

Applicable Elements

ElementSupports ontimeupdate?Notes
<video>YesMost common use case
<audio>YesPodcast players, music UI
<source>NoPut handler on parent media element
<track>NoListen on video/audio instead
<input>NoNot a media time event

ontimeupdate vs related media events

Event / attributeWhen it firesTypical use
ontimeupdatecurrentTime changes during playProgress bar, elapsed clock
ondurationchangeduration metadata availableTotal length label
onseekedUser/script seek completesJump to chapter
onprogressBuffered ranges changeBuffer indicator
onplay / onpausePlayback starts / stopsPlay button state

Examples Gallery

Inline ontimeupdate time display, dynamic element.ontimeupdate, and a progress bar with addEventListener.

👀 Live Preview

Play the video — elapsed time updates via timeupdate:

Current time: 0.0s

Example — Inline ontimeupdate

Show current playback seconds with a named function:

ontimeupdate-inline.html
<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>
Try It Yourself

How It Works

Each timeupdate event calls updateTime(), which reads currentTime and refreshes the display.

Dynamic Values with JavaScript

Assign ontimeupdate programmatically:

dynamic-ontimeupdate.html
<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>
Try It Yourself

How It Works

Setting the property replaces any previous ontimeupdate handler. Use addEventListener if you need multiple listeners.

Example — Progress bar with addEventListener

Update a range input as playback advances (modern pattern):

timeupdate-progress.html
<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>
Try It Yourself

How It Works

addEventListener("timeupdate") is the production-friendly way to drive custom progress UI.

♿ Accessibility

  • Label custom controls — Progress sliders need aria-label or visible labels.
  • Use aria-live sparingly — Announcing every timeupdate can overwhelm screen reader users; update visually instead.
  • Keep native controlscontrols on video/audio provides accessible playback UI out of the box.
  • Keyboard seek — Custom seek bars must support keyboard and focus states.
  • Captions — Pair timed UI with <track kind="captions"> for accessible media.

🧠 How ontimeupdate Works

1

Media plays

Browser advances currentTime.

Playback
2

timeupdate fires

Several times per second.

Event
3

Handler runs

Read currentTime, update UI.

Handler
=

Live progress UI

Clock and seek bar stay in sync.

Browser Support

The timeupdate event and ontimeupdate handler are supported in all modern browsers on HTML5 audio and video.

HTML5 · Fully supported

Universal media time events

All major browsers fire timeupdate during media playback.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
ontimeupdate / timeupdate Excellent

Bottom line: Safe for custom media players in all modern browsers.

💡 Best Practices

✅ Do

  • Use timeupdate for progress bars and elapsed time labels
  • Prefer addEventListener("timeupdate", …) in production code
  • Keep handler logic fast — avoid heavy DOM on every tick
  • Check duration is valid before dividing for percentages
  • Pair with ondurationchange for total length display

❌ Don’t

  • Assume timeupdate fires every frame — it does not
  • Run expensive analytics on every timeupdate without throttling
  • Use inline handlers for large applications — separate JS modules
  • Forget controls or custom accessible player UI
  • Confuse timeupdate with progress (buffering)

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about ontimeupdate

Bookmark these when building media players.

5
Core concepts
🎧 02

audio + video

Elements.

Scope
📈 03

currentTime

Seconds.

Property
04

addEventListener

Preferred.

Modern
05

Stay light

Fast handlers.

Perf

❓ Frequently Asked Questions

It runs JavaScript when the timeupdate event fires — as currentTime changes during audio or video playback.
<audio> and <video>. Attach the handler to the media element itself.
Roughly several times per second while playing (browser-dependent, often about 4 Hz). Not on every rendered frame.
ontimeupdate tracks playback position. ondurationchange fires when total duration metadata is known or changes.
Use addEventListener("timeupdate", handler) in production. Inline ontimeupdate is fine for tutorials and quick demos.
Yes in all modern browsers with HTML5 media support.

Build custom media players

Practice ontimeupdate with live time displays and progress bars in the Try It editor.

Try it yourself →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

3 people found this page helpful