简体   繁体   English

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

[英]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. 当我更换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>

你正在寻找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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 幻灯片不会重复使用 SetTimeout 功能 - Slideshow will not repeat with SetTimeout function Javascript setTimeout函数重复 - Javascript setTimeout function repeat 比使用多个 if 更好的方法 - Better way than using multiple if Javascript的多功能setTimeout同步问题,还是有更好的方法? - Javascript multiple function setTimeout syncing problems, or is there a better way? 有没有比JavaScript中的setTimeout更好的东西? - Is there something better than setTimeout in JavaScript? 为什么不能将函数调用(而不是函数引用或匿名函数)传递给setTimeout()? - Why can I not pass a function call (rather than a function reference or an anonymous function) to setTimeout()? angularjs ng-repeat 使用索引而不是名称 - angularjs ng-repeat using index rather than names 有没有更好的方法来检测用户的在线状态,而不是使用 setInterval 继续向服务器发送 AJAX 请求? - Is there any better way to detect a user's online status rather than keeping sending AJAX requests to the server by using setInterval? jQuery函数使用包含setTimeout的每个循环调用自身,而不是立即结束 - jQuery function calling itself with an each loop containing a setTimeout happens immediately rather than at the end 在测试JavaScript时,还有比setTimeout更好的方法来等待asnyc回调吗? - Any better way than setTimeout to wait for asnyc callbacks when testing JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM