Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

349
Views
Any way to get a currentTime value prior to the seek on HTMLMediaElement?

Let's say our app is using the default video player on Safari.

When a user is playing a video and then attempts to move to a different position of the video using the seek bar, it seems like pause event is fired first, and then we'll get seeking and seeked events fired.

I am wondering if we can get the currentTime value prior to the seek. For instance, assuming that a user jumps from t = 7 to t = 42 using the seek bar, I want to get 7 as the currentTime value somehow.

I expected that we could get this value by accessing currentTime property inside the pause event handler that is invoked right after the seek like the following:

const video = document.querySelector('#myvideo');
video.addEventListener('pause', () => {
  // I expected that the `video.currentTime` here has the "previous" position,
  // but it already points to the new position
  console.log(video.currentTime);
});

but unfortunately the currentValue was already updated to the new value at that point.

Is there any good way to achieve it?

(EDIT) Caching currentTime manually doesn't help, because apparently a timeupdate event fires before a pause event. More specifically, taking the following code as an example, when a user attempts to jump to another position, cache and currentTime printed within the pause handler seem always identical.

<!DOCTYPE html>
<html>
  <body>
    <video
      id="myvideo"
      width="640"
      height="360"
      controls
      src="video.mp4"
    ></video>
  </body>
  <script>
    const video = document.querySelector("#myvideo");
    let cache = 0;

    video.addEventListener("timeupdate", () => {
      cache = video.currentTime;
    });

    video.addEventListener("pause", () => {
      console.log({ cache, currentTime: video.currentTime });
    });
  </script>
</html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think @Kaiido means this when saying "Cache two values".
Code is untested (but looks better than being kept in comments section)

<script>
    const video = document.querySelector("#myvideo");
    let cache = 0;
    let cache_prev = 0;

    video.addEventListener("timeupdate", () => {
      cache_prev = cache; //# save last known value
      cache = video.currentTime; //# before updating to new currentTime
    });

    video.addEventListener("pause", () => {
    console.log("cache_prev : " + cache_prev );
    console.log("cache : " + cache );
    console.log("currentTime : " + video.currentTime );
    });
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!