I have code similar to this:
var count = 0;
setInterval(function() {
if(count===0){
console.log(123);
count++;
} else if(count>0) {
for(i=1;i<=2;i++){
(function(ind) {
setTimeout(function() {
console.log(ind);
}, 1000 * ind);
})(i);
}
count=0;
}
},1000)
The result is not what I expected, what I'd like to achieve for the console log is like:
123
1
2
123
1
2
...
and so on for each interval with 1000ms. Also I'd like to ask is there a better way to do this? The number of setTimeout loop(2 for the above case) can/might be different each time.
setTimeoutwill sometimes be longer than 1000ms. What then?