简体   繁体   中英

Is there a better way to repeat a function, rather than using setTimeout

Is there a better way to repeat a function infinite times?

Rather than using:

    setTimeout(myfunction(), 10)

How would I add that into this? When I replaced setTimeout it went weird with the order.

    <img id="slideshow" src="4.JPG" alt="cheese" width="264" height="264"/>
            <script>
                source=["1.jpg", "2.JPG", "3.JPG", "4.JPG"];
                var i=0
                function show(){
                document.getElementById("slideshow").src = source[i];
                if (i<source.length - 1)                
                i++
                else
                i=0
                setTimeout("show()",2500)                       
                }
                show()
            </script>

你正在寻找window.setInterval(function, 1000);

(function(){

  var images = ['1.jpg', '2.jpg', '3.jpg', '4.jpg'],
      img = document.getElementById('blerg');

  setInterval(function(){

    img.src = images[0];

    images.push(images.shift());

  }, 2500)

})();

while(true) hangTheBrowser();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM