Zusee Weekin
Zusee Weekin

Reputation: 1358

Angularjs $timeout not executing

I'm new to Angularjs and want to add a timer once the page load. I read a number of similar kind of questions here but still I couldn't resolve my issue. I added data-ng-init="init()" to my view. This is the controller:

    'use strict';

    angular.module('tempApp')
      .controller('MainController',['$scope','$timeout','dService', function ($scope,$timeout, dService) {

        $scope.init =  function(){
            console.log("Safety checkStatus page load");
            $timeout(callAtTimeout(),3000);
        }


    function callAtTimeout(){
        console.log("Safety checkStatus ***");
    }

 }]);

jsfiddle: https://jsfiddle.net/Zusee/s6xy48t0/9/

Here dService is separate service js file I'm going to use. Any help would be appreciated.

Upvotes: 0

Views: 2824

Answers (6)

RIYAJ KHAN
RIYAJ KHAN

Reputation: 15290

Why you need $timeout if you want to set timer.

$timeout will execute once.

Try with $interval.

Here is the changes :

$scope.init = function() {
          console.log("Safety checkStatus page load");
          $interval($scope.callAtTimeout, 1000);
        }
        $scope.callAtTimeout = function() {

          console.log("Safety checkStatus ***");
        }

Here is the working plunker

Upvotes: 0

ajin
ajin

Reputation: 1206

<!DOCTYPE html>
<html ng-app="tempApp">
<head>
    <title>Test</title>
    <script src="angular.min.js"></script>
</head>
<body ng-controller="MainController">
<script type="text/javascript">
       'use strict';

           angular.module('tempApp', [])
             .controller('MainController',['$scope','$timeout', function ($scope,$timeout) {

               $scope.init =  function(){
                   console.log("Safety checkStatus page load");

                   $timeout(callAtTimeout, 3000);
               };
               function callAtTimeout(){
                       console.log("Safety checkStatus ***");
                   }

$scope.init();
        }]);
</script>
</body>
</html>

Upvotes: 3

Suresh Gogu
Suresh Gogu

Reputation: 389

Problem is extra parenthesis in $scope.init()

$scope.init =  function(){
     console.log("Safety checkStatus page load");
     $timeout(function () {
          callAtTimeout();
     }, 3000);
};

Upvotes: 0

misss-popcorn
misss-popcorn

Reputation: 600

 'use strict';

        angular.module('tempApp')
          .controller('MainController',['$scope','$timeout','dService', function ($scope,$timeout, dService) {

            $scope.init =  function(){
                console.log("Safety checkStatus page load");
                $timeout(function () {
                  callAtTimeout()
                }, 3000);
            }  }]);


        function callAtTimeout(){
            console.log("Safety checkStatus ***");
        }

     }]);

You may also need to call $scope.init() explicitly

Upvotes: 0

Alvaro Heras
Alvaro Heras

Reputation: 81

You can try this?

'use strict';

    angular.module('tempApp')
      .controller('MainController',['$scope','$timeout','dService', function ($scope,$timeout, dService) {

       $scope.init =  function(){

            console.log("Safety checkStatus page load");

            $timeout(function () {
                  $scope.callAtTimeout();
            }, 1000);

       } 


       $scope.callAtTimeout = function(){
           console.log("Safety checkStatus ***");
       }

       $scope.init();
 }]);

Upvotes: 2

Makarov Sergey
Makarov Sergey

Reputation: 932

Add 'dService'

.controller('MainController',['$scope','$timeout', 'dService', function ($scope,$timeout, dService) {

And use like

$timeout(callAtTimeout,3000);

Upvotes: 0

Related Questions