How this code works? any one help me to understand ?
var myArray = ['one','two','three'];
var logAll = function (){
var long = myArray.length;
for(i=0;i< long;i++){
console.log(myArray[i]);
}
setTimeout(logAll,50);
}
logAll();
According to me, the myArray length is just 3, so, my log has to give me the result as 0,1,2. thats ok. out of my for loop i introduced a setTimeout for logAll function, but how that logAll function keep on calling the for loop instead of 3 times, to ever..? any one help me to understand this?
if so, why this is calling only one time ?
var myArray = ['one','two','three'];
var logAll = function (){
var long = myArray.length;
for(i=0;i< long;i++){
console.log(myArray[i]);
}
}
setTimeout(logAll,50);
You keep calling
setTimeoutat the end oflogAll, so it keeps callinglogAllevery 50ms.