I'm trying to put a delay in a forEach loop in AngularJS. With $timeout, they're firing once at the same time. With $interval, they're caught in an infinite loop, again at the same time.
I need the function delay between iterations of the forEach loop to fire the animations in delayed succession rather than at the same time.
...
vm.questions = [
{correct:false,animateDemo:false},
{correct:true,animateDemo:false},
{correct:true,animateDemo:false}
];
vm.questions.forEach(function(question, idx) {
// need a delay between animationDemo assignments
$timeout(function(){
if (question.correct) {
question.animateDemo = true;
}
},1000);
});
I tried interval too, but that causes an infinite loop.
vm.questions = [{correct:false}. {correct:true}, {correct:true}];
vm.questions.forEach(function(question, idx) {
// causes infinite loop
$interval(function(){
if (question.correct) {
question.animateDemo = true;
}
},1000);
});
I'm sure the solution is probably something simple, but getting it in this syntax is a bit confusing to me personally.