简体   繁体   English

如何使用setTimeout(),JavaScript

[英]How to use setTimeout(), JavaScript

I have added a "play" button to my image gallery slideshow, now when i press this button the pictures should change every 3000 mlsec for example. 我在图库幻灯片中添加了一个“播放”按钮,例如,当我按此按钮时,图片应每隔3000毫秒更改一次。

I know that i need use "setTimeout()" inside the for loop, while my var i will not reach the value of the array_pics.lenght. 我知道我需要在for循环内使用“ setTimeout()”,而我的var不会达到array_pics.lenght的值。 But i can't make it work. 但是我不能使它工作。

play.onclick = function play() {
    var i;
    var ind = index;
    document.getElementById('largeImg').src = arr_big[index].src;

    //Tryed to put slide() function inside the for loop which was inside setTimeout(), get a syntax errors.
    var slide = function slide() {
        i = ind;
        ind += 1;
        index = ind;
        document.getElementById('largeImg').src = arr_big[ind].src;
    };

    slide(); //I have tryed setTimeout(slide(), 3000) here inside the for loop. Does not work!

};

Can someone help me to make it work? 有人可以帮助我使它正常工作吗? How i can use setTimeout() and for loop? 我如何使用setTimeout()和for循环? Thanks. 谢谢。

If you want a repeated action it's often better to use setInterval() rather than setTimeout() and use clearInterval() when you want it to stop. 如果要重复操作,通常最好使用setInterval()而不是setTimeout()并在要停止时使用clearInterval()。 I would take the function definitions out of the onclick handler and make sure your index counters are global rather than local to the functions. 我将从onclick处理程序中删除函数定义,并确保您的索引计数器是全局的,而不是函数的局部计数器。 Just some starting points for you. 只是一些起点。

So something along the lines of ... 所以类似...

var index = 0;
var timer = 0;
var maxNumOfImages = arr_big.length;

play.onclick = playMe; //changed the function name to avoid confusion between button and function
stop.onclick = stopMe;

function playMe() {
    document.getElementById('largeImg').src = arr_big[index].src;
     timer = setInterval( slide, 3000 );
}

function slide() {
    index += 1;
    if (index == maxNumOfImages) {
       index = 0;
    }
    document.getElementById('largeImg').src = arr_big[index].src;
 }

function stopMe() {
    clearInterval( timer );
}

You can also use below method. 您也可以使用以下方法。

setTimeout(function () { slide(); }, 3000);

OR 要么

setTimeout(function () { slide() }, 3000);

You must call setTimeout into the slide function to repeat it each 3 seconds. 您必须将setTimeout调用到slide函数中,以每3秒重复一次。 And I think you don't need to name every of your functions. 而且我认为您不必为每个函数命名。

play.onclick = function() {
    var i;
    var ind = index;
    document.getElementById('largeImg').src = arr_big[index].src;

    //Tryed to put slide() function inside the for loop which was inside setTimeout(), get a syntax errors.
    var slide = function() {
        i = ind;
        ind += 1;
        index = ind;
        document.getElementById('largeImg').src = arr_big[ind].src;

        setTimeout(slide, 3000);
    };

    slide();

};

Edit : You can also use setInterval : 编辑:您也可以使用setInterval:

play.onclick = function() {
    var i;
    var ind = index;
    document.getElementById('largeImg').src = arr_big[index].src;

    setInterval(
        function() {
            i = ind;
            ind += 1;
            index = ind;
            document.getElementById('largeImg').src = arr_big[ind].src;
        },
        3000
    );
};

If you need to be abble to play / pause the cycle, you need to use setInterval. 如果您需要精通播放/暂停循环,则需要使用setInterval。

Example

(function() {
   var arr_big = [
    {src:"http://placehold.it/350x150"},
    {src:"http://placehold.it/350x250"},
    {src:"http://placehold.it/350x350"}    
   ];

 var iid, index;

function  slide () {        
   index = (index+1 == arr_big.length) ? 0 : index +1;
   document.getElementById('largeImg').src = arr_big[index].src;
}

playBtn.onclick = function () { 
   index = 0
   iid = setInterval(slide       , 500);
}
stopBtn.onclick = function() {
  clearInterval(iid)
}
})();

HTML HTML

<button id="playBtn">play</button>
<button id="stopBtn">stop</button>
<p><img src="http://placehold.it/350x150" id="largeImg" /></p>

Here is a working jsfiddle 这是一个工作的jsfiddle

I solved it, here is my code. 我解决了,这是我的代码。

Thank you, guys! 感谢大伙们! You all helped me a lot! 你们都帮了我很多!

play.onclick = function play() {
    document.getElementById('largeImg').src = arr_big[index].src;

    var slide = function slide() {
        if(index > arr_big.length-5) {
             index = 0;
         }
         index += 1;
         document.getElementById('largeImg').src = arr_big[index].src;
         setTimeout(slide, 1000);                           
     };
     slide();
};

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

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