Intentional delaying in Angularjs -


i making little diagnose web page , want make system working hard in background showing loading image while.

i not programmer. on chunk of codes works , how tried... justdelaying function never delays later process. seems process running simultaneously.

app.controller('hardworkctrl', function($scope, $timeout) {      $scope.hardwork = function() {          // start showing hard-working image         $scope.loading = true;          $scope.justdelaying = function() {             $timeout(function() {                 // nothing...             }, 5000);         }         $scope.justdelaying();          $scope.theanswer = "42."          // stop showing hard-working image         $scope.loading = false;      }; }; 

any idea?

anything happens inside "$timeout" asynchronous, it's scheduled later, without blocking main execution. in other words, instructions after call $scope.justdelaying() happen immediately, while instructions inside justdelaying delayed 5 seconds. make instructions execute later, need move them inside $timeout, this:

app.controller('hardworkctrl', function($scope, $timeout) {      $scope.hardwork = function() {          // start showing hard-working image         $scope.loading = true;          $scope.delaythendostuff = function() {             $timeout(function() {                 $scope.theanswer = "42."                  // stop showing hard-working image                 $scope.loading = false;             }, 5000);         }         $scope.delaythendostuff();     }; }; 

Comments