angular-timer
angular-timer copied to clipboard
Not able to update timer value inside ajax success response block.
Hi, I want to put the timer value from my db, so that's why I am making a ajax call and the response will replace the value of the timer. but its not working, here is my code:
<timer start-time="startTime" interval="100" countdown="students.countdown"> <strong>{{hours}} hours, {{minutes}} minutes, {{seconds}} second</strong> </timer>
dashboardFactory.getTimerTime().then(function(response) { $scope.startTime = response.time; });
You cannot set the timer's start time from an asynchronous call after directive compilation (page load). You need to re-compile the directive after the async response & attach it to the scope. Here is a plunkr that kinda does that - http://plnkr.co/edit/GjKSSVM1x2UQC2gLQeGo?p=preview
The original issue https://github.com/siddii/angular-timer/issues/34#issuecomment-30958303 associated to that plunkr.
Thanks, its working but timer is still not working properly, actually when user logged I stored the new Date().getTime() + 180*60*1000 in my DB and the stored value look something like this 1387872383960. So the timer value initialize from DB, it will remain same if you refresh the page, but when I refreshed the page after some time like after 10 minutes then it also refreshed the timer. Please help me Siddi and let me know if I am doing something wrong.
Siddii, any solution for above mentioned problem. Please give me any suggestion for that.
Can you send a plunkr for your issue? I don't quite get whats happening here.
Actually I want to create a 3 hours countdown timer for Online Exam which should not reset on page refresh. When user logged in I am storing timer start-time value in DB. So start time will comes from DB.
Please check this plunk and help me to out of it http://plnkr.co/edit/DaQdtOj7OcT4BaJuqYAo?p=preview
I don't see anything new in that plunkr. It looks like the same one I showed earlier.
Note that when you refresh the page/view the controller gets initialized & display values gets reset whether in a directive or not. I am guessing you probably need to store the displayed time in the DB & re-initialize directive on page load again.
Yes you are right, I am storing the timer value (in milliseconds) in DB and initialize it on page load, So the value will remain same on every page load, but my countdown timer is not working. I want a 3 hours countdown timer for online exam.
You could also use ng-if on your timer element to check for your startTime variable. This way, the directive will only be initialised when you have your date, and it will work.
@Apercu thanks for the answer, i will refer to your answer in some other issues.
The example does not work. =) Working example: http://plnkr.co/edit/QFEdzpOhRbInH7HQzLVr?p=preview