Monirul Islam
Monirul Islam

Reputation: 935

Javascript Loop Delay needed

I Need 3 second delay after 2nd loop. place is commented out. pleaes help.

var x = [[1,2,3,4,5,6,7],[8,9,10,11,12,13,14],[15,16,17,18,19,20,21],[22,23,24,25,26,27,28],[29,30,31,32,33,34,35],[36,37,38,39,40,41,42],[43,44,45,46,47,48,49]];
var j=indx='';
var n = 7;
var slice = j= indx='';
for (slice = 0; slice < 2 * n - 1; ++slice) {
    var z = slice < n ? 0 : slice - n + 1;
    for (j = z; j <= slice - z; ++j) {
        indx = x[j][slice - j]-1;
        console.log(indx);
    }
    //window.setTimeout("", 1000);
    //i need delay here.----------------------
}

Upvotes: 3

Views: 89

Answers (1)

shift66
shift66

Reputation: 11958

use setInterval(callback, time). It's the same as setTimeout but function will be called forever.
clearInterval should be called with your interval's id to stop execution.
setInterval function returns that id

var intervalId = setInterval(function(){
    var z = slice < n ? 0 : slice - n + 1;
    for (j = z; j <= slice - z; ++j) {
        indx = x[j][slice - j]-1;
        console.log(indx);
    }
    if(++slice >= 2*n-1)
        clearInterval(intervalId);
}, 3000);

Upvotes: 1

Related Questions