javascript - Reset Angular Countdown Directive -
i have countdown directive counting down initial value that's in element 0 once modal warns user session timeout. once countdown reaches 0 user prompted modal either exit, or continue. if hit continue, if same session expiration warning modal, counter 0. can't quite figure out how reset counter directive without returning , stopping on future warnings. here's code have now.
.directive("ndcountdown", function($timeout, $compile){ return{ restrict: 'a', link: function(scope, element, attrs){ var countdown = function(){ var timer = element.find('strong'); for(var = 0; < timer.length; i++){ var secs = parseint(timer.text(), 10) - 1; if(secs >= 0){ timer.text(secs); } else{ //reset element original value //stop timeout in session have continue //work in future dialogs. } } $timeout(countdown, 1000); } countdown(); } } });
first step save variable of inside originally. run timeout if not @ end.
.directive("ndcountdown", function($timeout, $compile){ return{ restrict: 'a', link: function(scope, element, attrs){ var timer = element.find('strong'), max = parseint(timer.text()); var countdown = function(){ for(var = 0; < timer.length; i++){ var secs = parseint(timer.text(), 10) - 1; if(secs >= 0){ timer.text(secs); $timeout(countdown, 1000); } else{ timer.text(max); } } } countdown(); } } });
now if needed able restart it, want communicate controller. maybe $broadcast this. be sure inject $rootscope
$rootscope.$broadcast('event:start-countdown');
then in directive add @ end of link function, , inject $rootscope. again.
$rootscope.$on('event:start-countdown', function() { countdown(); }
Comments
Post a Comment