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

151
Visualizações
Why doesn't the video stop when the "currentTime>=maxTime" in Javascript?

Look at this code

I tried to play the video from a specific time to another specific time.

<html>
<head>

<script>
function playVideo(str) { 
  var video = document.getElementById("myVideo");
  video.src = str;
  video.currentTime=16;
  video.play();

  var maxTime = 20;
  video.addEventListener("progress", function(){
    if(video.currentTime >= maxTime){
     video.pause();  
    }
  }, false);
} 

</script>
</head>
<body>


<video id="myVideo" width="1000" height="800" controls autoplay loop>

  Your browser does not support the video tag.
</video>


<a  onclick="playVideo('video/journey27.mp4')" > Play2 </a>

</body>
</html>

Why doesn't the video stop when the currentTime>=maxTime?

i think there is something wrong with the eventListener.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to use the "timeupdate" event listener that executes whenever the time changes and not the progress which run on different situations.

Your playVideo function should look something like this:

function playVideo(str) {
  var video = document.getElementById("myVideo");
  video.src = str;
  video.currentTime = 16;
  video.play();

  var maxTime = 20;
  video.addEventListener(
    "timeupdate", // changed the event listener
    function (event) {
      if (this.currentTime >= maxTime) {
        video.pause();
      }
    },
    false
    );
  }

Worth to mention that using an instance of the object that you're creating an event listener to him is not always a best practice and better to use this key word.

about 4 years ago · Santiago Trujillo 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