有没有更好的方法来重复一个函数,而不是使用setTimeout

时间:2013-01-09 01:09:37

标签: javascript

有没有更好的方法无限次重复一个函数?

而不是使用:

    setTimeout(myfunction(), 10)

我如何将其添加到此?当我更换setTimeout时,它对订单感到奇怪。

    <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>

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

(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)

})();

答案 2 :(得分:-1)

while(true) hangTheBrowser();

相关问题