var arr = [[1,2,3],[4,5,6],[7,8,9]];
function out(ar){
var interval = setInterval(function(){
for (var i=0; i<ar.length;i++){
for (var j=0;j<ar[i].length;j++){
document.write(ar[i][j]);
}
document.write("</br>");
}
clearInterval(interval);
},1000);
}
out(arr);
This code works, but i would like to make one second delay before outputting every number.
The above code cannot do that. How do i do that in vanilla Javascript?
setIntervalisn’t guaranteed to get you 1 second spacing because other things might run, but you can get close with the followingPut the array indices outside the function, so each time the interval fires, it gets the loop state from the last run.
Also, you can’t use
document.writeinside an interval handler because callingdocument.writeafter the document has closed clobbers the existing document.