How to increment the counter every second? The increment should not stop even when I refresh it on browser. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to increment the counter every second? The increment should not stop even when I refresh it on browser.

I'm writing a .JS program that increments every second and shud keep on running because this is not something connected to browser. I have an admin page created and I have created a sample user page. I'm planning to place this .JS file in my user's. Any help how to do it.

5th Oct 2017, 6:56 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
16 Answers
+ 2
Here you go Added Pause/Start button https://code.sololearn.com/W7B3k8ZufM16/?ref=app This solution is not perfect, you should implement localStorage to store timer variable for each different users. Currently the timer resume value is jumping to current time tracking due to not keeping track of counter previously paused value.
5th Oct 2017, 9:56 AM
Calviղ
Calviղ - avatar
+ 10
Interesting solution by @Calvin again! @Muthiah basically it compute the relative time from a predefined moment and that's why it's able to persist the counter. Otherwise you'll need to store it somewhere else like @Baptiste mentioned. 😉
5th Oct 2017, 7:25 AM
Zephyr Koo
Zephyr Koo - avatar
+ 4
For different increment, you need to multiply an increment constant with counter value. https://code.sololearn.com/WYKLBfHPP5Kj/?ref=app
5th Oct 2017, 8:37 AM
Calviղ
Calviղ - avatar
+ 3
@Zephyr Thank you for the explanations. 👍🏼 @Muthiah If you need to save local file counter status, you could use localStorage.
5th Oct 2017, 8:36 AM
Calviղ
Calviղ - avatar
+ 2
I am not proficient with JS, but you could save a file with the count every seconds and try to read it first when you load your page You could add the date of when the second occured so that you start again at 0 if it was too long ago
5th Oct 2017, 7:07 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
Thank you so much! :) @Calvin
5th Oct 2017, 8:41 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
+ 2
I cannot thank you enough!! Thanks a ton!! @Calvin
5th Oct 2017, 10:07 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
+ 1
Thank you! @Calvin! Can you kindly explain me, please!?
5th Oct 2017, 7:17 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
+ 1
Oh okay!! now I get it!! Thank you! @Zephry
5th Oct 2017, 7:30 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
+ 1
You need to add pause and resume timer. I try to look into it later.
5th Oct 2017, 9:08 AM
Calviղ
Calviղ - avatar
0
How to save a file with the count every second?
5th Oct 2017, 7:11 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
0
I have another doubt what if I have to increment a variable by 0.0018 every second. how should I do it? (PS: I am a newbie to JS)
5th Oct 2017, 8:15 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
0
I'm really sorry to bother again and again only after getting to understand all this I am getting doubts. What if I had to stop this at a certain time stamp!? And also can you kindly let me know how shud I place this in the user dashboard and have control over it in the admin page?
5th Oct 2017, 8:56 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
0
Thank you once again @Calvin. I will also try and will get back to you. Good day! :)
5th Oct 2017, 9:12 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar
0
@Calvin how to set up a start time button after inserting the time stamp and place it in a DB?
7th Oct 2017, 3:04 AM
Muthiah Abbhirami
Muthiah Abbhirami - avatar