Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

347
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda