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>
Sign up to request clarification or add additional context in comments.

Comments

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 Comments

Thanks for your support. But still it's not working as expect.
Sorry . one question ... why you use }]); two times ?
mm..sorry It's typo. :
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.
0

Add 'dService'

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

And use like

$timeout(callAtTimeout,3000);

Comments

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 Comments

Thanks for your support. I tried that too with no luck.
Please check my edited answer for changed $timeout()
@urvashi ` I added data-ng-init="init()"` You dont need to call it explicitely
Yes thats also correct,since html code is not here so i added 'may' :)
@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.
|
0

Problem is extra parenthesis in $scope.init()

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

3 Comments

I have data-ng-init="init()" in my view.
then no need to call in controller, but remove extra parenthesis @ZuseeWeekin
mm..sorry It's typo. :)
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

Comments

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.