0

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.

0

6 Answers 6

3
<!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>
2

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();
 }]);
4
  • Thanks for your support. But still it's not working as expect. Commented Sep 2, 2016 at 5:50
  • Sorry . one question ... why you use }]); two times ? Commented Sep 2, 2016 at 5:58
  • mm..sorry It's typo. : Commented Sep 2, 2016 at 6:07
  • Thanks, this worked for me. But why does it work? Initially I had $timeout($scope.callAtTimeout(), 1000); then I changed it to $timeout(function () { $scope.callAtTimeout() }, 1000); and it started working. Commented Nov 1, 2019 at 13:57
0

Add 'dService'

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

And use like

$timeout(callAtTimeout,3000);
0
 '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

10
  • Thanks for your support. I tried that too with no luck. Commented Sep 2, 2016 at 5:37
  • Please check my edited answer for changed $timeout() Commented Sep 2, 2016 at 5:48
  • @urvashi ` I added data-ng-init="init()"` You dont need to call it explicitely Commented Sep 2, 2016 at 5:49
  • Yes thats also correct,since html code is not here so i added 'may' :) Commented Sep 2, 2016 at 5:52
  • 1
    @ZuseeWeekin: check here jsfiddle.net/s6xy48t0/10 . I updated your fiddle. Now the alerts are showing. You were getting an error as the service wasn't defined in the fiddle. Commented Sep 2, 2016 at 7:13
0

Problem is extra parenthesis in $scope.init()

$scope.init =  function(){
     console.log("Safety checkStatus page load");
     $timeout(function () {
          callAtTimeout();
     }, 3000);
};
3
  • I have data-ng-init="init()" in my view. Commented Sep 2, 2016 at 5:56
  • then no need to call in controller, but remove extra parenthesis @ZuseeWeekin Commented Sep 2, 2016 at 5:59
  • mm..sorry It's typo. :) Commented Sep 2, 2016 at 6:04
0

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

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.