简体   繁体   English

如何反复调用JavaScript函数

[英]How to call JavaScript function repeatedly

I am trying to call the JavaScript function repeatedly after the execution. 我试图在执行后重复调用JavaScript函数。

function startSlide(){
        var products = [
                        ['images/product_images/footwear/footwear_1.jpeg'],
                        ['images/product_images/mobile/mobile_1.jpeg'],
                        ['images/product_images/camera/camera_1.jpeg'],
                        ['images/product_images/fashion/fashion_1.jpeg'],
                        ['images/product_images/laptop/laptop_1.jpeg'],
                        ['images/product_images/furniture/furniture_1.jpeg']
                       ];
        for (var i = 0; i < products.length; i++) {
            var image = products[i][0];
            imgslider(image, i * 800);
        }
    };

    function imgslider(image, timeout)
    {
          window.setTimeout(function() {
          document.getElementById('imgslider').innerHTML = "";
          var product = document.getElementById('imgslider');
          var elem = document.createElement("img");
          product.appendChild(elem);
          elem.src = image;
          },timeout);
          startSlide();
    }

The startSlide() function iterates the array and fetch the value from array and call the function imgslider() . startSlide()函数迭代数组并从array中获取值,然后调用函数imgslider() the imgslider() function appends the image to the div. imgslider()函数将图像添加到div。

at the end of the imgslider() i am trying to call the startSlide() so that it could continue the execution.... but its not working. 在imgslider()的末尾,我试图调用startSlide(),以便它可以继续执行...。但是它不起作用。 how can i do this? 我怎样才能做到这一点?

The problem is your code is creating an infinite recursion... 问题是您的代码正在创建无限递归...

Maintianing your code structure you can add a flag to fix the problem 维护代码结构,您可以添加一个标志来解决问题

 function startSlide() { var products = [ ['//placehold.it/64X64&text=1'], ['//placehold.it/64X64&text=2'], ['//placehold.it/64X64&text=3'], ['//placehold.it/64X64&text=4'], ['//placehold.it/64X64&text=5'] ]; for (var i = 0; i < products.length; i++) { var image = products[i][0]; imgslider(image, i * 800, i == products.length - 1); } }; function imgslider(image, timeout, last) { window.setTimeout(function() { document.getElementById('imgslider').innerHTML = ""; var product = document.getElementById('imgslider'); var elem = document.createElement("img"); product.appendChild(elem); elem.src = image; if (last) { setTimeout(startSlide, 800); } }, timeout); } startSlide() 
 <div id="imgslider"></div> 


If you want to loop, then you can use setInterval() instead 如果要循环,则可以使用setInterval()代替

 function startSlide() { var products = [ ['//placehold.it/64X64&text=1'], ['//placehold.it/64X64&text=2'], ['//placehold.it/64X64&text=3'], ['//placehold.it/64X64&text=4'], ['//placehold.it/64X64&text=5'] ], i = 0; setInterval(function() { imgslider(products[i][0]); if (++i >= products.length) { i = 0 } }, 800); }; function imgslider(image) { document.getElementById('imgslider').innerHTML = ""; var product = document.getElementById('imgslider'); var elem = document.createElement("img"); product.appendChild(elem); elem.src = image; } startSlide() 
 <div id="imgslider"></div> 

That's because window.setTimeout only calls the function once, so you should use window.setInterval instead. 这是因为window.setTimeout仅调用一次函数,因此您应该改用window.setInterval Something like this: 像这样:

window.setInterval(function () { /* code to update picture... */ }, 3000);

If it's still not working you can check the console log for errors. 如果仍然无法正常工作,则可以检查控制台日志中是否有错误。
That's F12 in IE or Ctrl + Shift + J in Chrome. 在IE中为F12 ,在Chrome中为Ctrl + Shift + J

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

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