Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change "countdown" attribute from AngularJS controller? #21

Closed
jasonshah opened this issue Sep 15, 2013 · 19 comments
Closed

Change "countdown" attribute from AngularJS controller? #21

jasonshah opened this issue Sep 15, 2013 · 19 comments

Comments

@jasonshah
Copy link

Hi,
Is there a way to change the "countdown" attribute from the AngularJS controller?

I tried something like this:

index.html

<timer interval="1000" countdown="{{timerTime}}">{{seconds}}</timer>

controller.js

controller('WorkoutCtrl', ['$scope', function($scope) {
    $scope.timerTime = 15;
    ...
}]);

but I receive an error:
"Error: Syntax Error: Token 'timerTime' is unexpected, expecting [:] at column 3 of the expression [{{timerTime}}] starting at [timerTime}}]."

Thanks for the great Directive!

@siddii
Copy link
Owner

siddii commented Sep 18, 2013

I think this is what you are looking for...

<!DOCTYPE html>
<html>
<head>
    <title>AngularJS Timer Countdown Scope Variable</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="http://siddii.github.io/angular-timer/app/js/timer.js"></script>
    <script>
        angular.module('MyApp', ['timer']);
        function MyAppController($scope) {
            $scope.countdownVal = 600000;
        }
        MyAppController.$inject = ['$scope'];
    </script>
</head>
<body ng-app="MyApp">
    <div ng-controller="MyAppController">
        <timer interval="100" countdown="countdownVal"/>
    </div>
    <br/>
</body>
</html>

@jasonshah
Copy link
Author

I think I found a similar solution (apologies for not posting it earlier!):

index.html:

    <timer interval="1000" countdown="countdownVal">{{seconds}}</timer>

controller.js:

    controller('WorkoutCtrl', ['$scope', function($scope) {
        $scope.$apply(function() {
            $scope.countdownVal = 15;
        });
        $scope.$broadcast('timer-start');
    }]);

It does make me nervous that the broadcast appears to happen before applying the countdownVal, but perhaps that's just my not understanding AngularJS's digest lifecycle. Anyway, it works.

@362E
Copy link

362E commented Mar 14, 2014

Hey guys.

I have a problem that takes the problem stated on this thread one step further.

So, I too have an api call. However, that api call returns a list of objects. Each of those objects has a different "seconds to closing time" associated with it.

Any hints on how I can initialize multiple timers on the same page, each seeded with its own countdownval ?

So, I am looking to do something like this.

<div ng-controller="MyAppController">
    <timer interval="100" countdown="countdownVal1"/>
    <timer interval="100" countdown="countdownVal2"/>
</div>

and my object will have the structure
[{countdownval1:time1...},{countdownval2:time2...}...]

@siddii
Copy link
Owner

siddii commented Mar 14, 2014

Here is the code...

<!DOCTYPE html>
<html>
<head>
    <title>AngularJS Timer Countdown Scope Variable</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="http://siddii.github.io/angular-timer/app/js/timer.js"></script>
    <script>       
        angular.module('MyApp', ['timer']);
        function MyAppController($scope, $timeout) {
        $timeout(function() {
            console.log("Init countdowns");
            $scope.countdowns = [{countdownval:100},{countdownval:200}, {countdownval:300}];            
            }, 3000);   
        }
        MyAppController.$inject = ['$scope', '$timeout'];
    </script>
</head>
<body ng-app="MyApp">
    <div ng-controller="MyAppController">

    <div ng-repeat="countdown in countdowns">
        <div>Value = <div><timer interval="1000" countdown="countdown.countdownval">{{countdown}}</timer></div></div>
    </div>
    </div>
</body>
</html>

@joelsalisbury
Copy link

Strangely, when using any of these, in conjunction with {{minutes}} or {{seconds}}, the counter starts over after it counts down to 0. has anyone else run into this?

Edit: What I mean is the counter actually starts counting UP from the original value!

@siddii
Copy link
Owner

siddii commented Jun 18, 2014

Should be stopping according to the code - https://github.com/siddii/angular-timer/blob/master/app/js/timer.js#L238

Can you create a plunkr for the issue?

@joelsalisbury
Copy link

Thank you for the advice. My app (fairly large) contained another object named "timer" in the scope. It was obviously causing all sorts of craziness. Thanks again!

@kaplievabell
Copy link

Hello, I was wondering if you can give me an advise on how to reset the countdown clock to new value after It has been stopped? I am starting and stopping it based on the command sent from it from backed via websocket. However, I am stumbled on how to reset it to new or default values. Maybe you have an advise? Thank you.

var proAmControllers = angular.module('proAmControllers', ['proAmServices', 'timer']);

proAmControllers.controller('ProAmCtrl',
[
'$scope', '$rootScope', '$http', 'webSocketService', '$filter',
function($scope, $rootScope, $http, webSocketService, $filter) {
$http.defaults.useXDomain = true;

            var data = {};

            $scope.myCountdown = 45;
            $scope.myAutoStart = false;

            webSocketService.getData();

            $rootScope.$on('broadcast', function() {
                data = webSocketService.message;
                $scope.tournament = data;
                $scope.eventState = data.event.eventState;

                $scope.lastComd = $scope.eventState.lastCommand;

                $scope.$watch('lastComd', function() {
                    if ($scope.lastComd === '1') {
                        $scope.$broadcast('timer-start');
                    }
                    else if ($scope.lastComd === '2') {
                        $scope.$broadcast('timer-stop');
                       // I thought this would resent countdown but it doesnt.
                        $scope.myCountdown = 45;
                        $scope.myAutoStart = false;
                    }
                });
            });
        }]);
:{{countdown}}

@siddii
Copy link
Owner

siddii commented Aug 7, 2014

No, it won't. You probably need to re-init the directive using $compile. Kinda like whats done here.. http://plnkr.co/edit/SCAznyak4Sul0WDG5oMC?p=preview

The problem is we don't have a proper reset for countdown until then, you should do something like that.

@kaplievabell
Copy link

Thank you

@zeeshanjan82
Copy link

Thanks @siddii I tried your solution to refresh the counter but its not working. I have created a plunkr
http://plnkr.co/edit/Lat9ig?p=preview
I am not able to refresh the counter once set I even tried @jasonshah solution which also does not work. Why i am not able to $apply on the value and I get the error $digest under progress?

@zeeshanjan82
Copy link

Done. Found this line of code from the source code which really helped:

      $scope.$broadcast('timer-set-countdown', 60);

@siddii
Copy link
Owner

siddii commented Oct 31, 2014

Cool.. I am glad you found a fix :)

@mattkrebs
Copy link

My timer markup looks like this

<timer autostart='false' interval="1000" countdown="countdown">{{mminutes}}:{{sseconds}}</timer>

The above solution is working to update {{countdown}} but it doesnt update {{mminutes}} or {{sseconds}}

Any thoughts on this?

Thanks for creating this directive!

@rahmanict
Copy link

is it your js or github?

<script src="http://siddii.github.io/angular-timer/app/js/timer.js"></script>

@samoeba
Copy link

samoeba commented Oct 27, 2015

@zeeshanjan82 @siddii has this line of code gone away? $scope.$broadcast('timer-set-countdown', 60); I ran into the code below in the source code and I'm wondering what it does. How is it invoked?

$scope.$on('timer-set-countdown', function (e, countdown) {
    $scope.countdown = countdown;
});

@OlympicLarry
Copy link

var value = 1800;
$scope.$broadcast('timer-set-countdown-seconds', value);

@meShakti
Copy link

meShakti commented Jul 8, 2016

I am having the same issues as @mattkrebs

@HeenaOberoi
Copy link

I have a timer as below which works fine for Chrome and Firefox,but skips a second many times in IE.

{{mminutes}}:{{sseconds}}

In controller,
$rootScope.OverAllTimeRemaining = 4000;//Added arbitary value which ideally is dynamic-This time is in seconds

The timer is running fine yet skips seconds only in IE.

Any solution???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests