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

264
Views
recursive setTimeout() function not repeating in javascript

My setTimeout() function is not repeating after the initial one runs. It runs initially in the "Window.onload" but the second setTimeout() inside the "myHandler" function does not run

problem area: var timeout;

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

function myHandler() {
    i++;
   timeout = setTimeout(myHandler, 10000)
}   

full code in 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

I ran the code locally and I got some errors on videoPlay function. I think nothing wrong on setTimeout, but when the javascript crashes, the function don't run again. Try to comment the videoPlay calls and code will run.

Consider to use setInterval instead setTimeout. And you don't need to call videoPlay on if and else, you can refact that to

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

0

I solved my own problem as it turns out:

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

are not functions with iframe which was not running the rest of my code.

Thank you all for your help and input.

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!