0

I want to use delay in my angular.foreach loop. I have a list and I want to display them one by one with 2 seconds periods.

 angular.forEach(vm.SimulationResult, function (value, key) {
                    timeout2 = $timeout(function () {
                        vm.Alerts.push({ msg: value.MainStatus });
                    }, 2000);
                });

what should I do ?

3
  • What is the structure of vm.SimulationResult? Commented Apr 5, 2016 at 10:06
  • Might be worth reading: stackoverflow.com/questions/3583724/… Commented Apr 5, 2016 at 10:06
  • is that important ? It is a list with primitive types. @SiddharthAjmera Commented Apr 5, 2016 at 11:16

1 Answer 1

5

Take advantage of the fact that $timeout returns a promise and chain the promises together so that each one starts another promise timeout that will push the next alert:

let promise = $timeout();
angular.forEach(vm.SimulationResult, function(value, key) {
     promise = promise.then(function() {
         vm.Alerts.push({ msg: value.MainStatus });
         return $timeout(2000);
     });
});

(make sure your version of angular is up to date as older versions didn't let you omit the callback function in a timeout)

Sign up to request clarification or add additional context in comments.

3 Comments

Worked for me (AngularJS 1.5.8)
Worked for me as well
Nice! Worked with Angular 1.6.2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.