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

268
Views
función recursiva setTimeout () que no se repite en javascript

Mi función setTimeout() no se repite después de que se ejecuta la inicial. Se ejecuta inicialmente en "Window.onload", pero el segundo setTimeout() dentro de la función "myHandler" no se ejecuta

área problemática: tiempo de espera de var;

 window.onload = function(){ timeout = setTimeout(myHandler, 5000); }; function myHandler() { i++; timeout = setTimeout(myHandler, 10000) }

código completo en HTML:

 <html> <body> <iframe id="myVideo" allowfullscreen="true" height="720" width="1280"></iframe> <script type="text/javascript"> var videoSource = new Array(); videoSource[0] = 'xxx'; videoSource[1] = 'xxx'; var i = 0; var videoCount = videoSource.length; var timeout; window.onload = function(){ timeout = setTimeout(myHandler, 5000); }; function myHandler() { alert('run') i++; if (i == (videoCount - 1)) { i = 0; videoPlay(i); } else { videoPlay(i); } timeout = setTimeout(myHandler, 10000) } function videoPlay(videoNum) { document.getElementById("myVideo").setAttribute("src", `${videoSource[videoNum]}&parent=localhost&autoplay=true&muted=false`); document.getElementById("myVideo").load(); document.getElementById("myVideo").play(); } videoPlay(0); // play the video </script> </body> </html>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Ejecuté el código localmente y obtuve algunos errores en la función videoPlay. Creo que no hay nada malo en setTimeout, pero cuando el javascript falla, la función no se vuelve a ejecutar. Intente comentar las llamadas de videoPlay y se ejecutará el código.

Considere usar setInterval en lugar de setTimeout. Y no necesita llamar a videoPlay on if y else, puede rehacer eso para

 if (i == videoCount - 1) i = 0 videoPlay(i)
about 4 years ago · Juan Pablo Isaza Report

0

Resolví mi propio problema como resulta:

 document.getElementById("myVideo").load(); document.getElementById("myVideo").play();

no son funciones con iframe que no estaba ejecutando el resto de mi código.

Gracias a todos por su ayuda y aporte.

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!