Display array elements with delay

前端 未结 8 1886
星月不相逢
星月不相逢 2021-01-06 18:50

I have an arrays=[John; Alex; Mark], I wanna to show the elements of this array one by one by 3 second delay.

for (var i=0; i<=3; i++)
  {
          


        
8条回答
  •  广开言路
    2021-01-06 19:18

    Try this without pseudo-recursion

    var arr = [10,20,30,40]; // your array
    var i = 0;
    var interval = 2000 // 2 sec, you can add your time required
    function callInterval() {
    // set a variable for setInterval
    	var time = setInterval(()=>{
    		console.log('['+arr[i]+','+i+']');
    		i++; 
    		if(i==arr.length){
    			this.clearInterval(time);// clear the interval after the index
    		}
    		}, interval);
    }
    
    callInterval();

提交回复
热议问题