I think u should use localstorage for store time. its better to store time on unload page event
Solution 3 :
The best way to persist a countdown is probably to save the end of the countdown as a fixed point in time (such as a UNIX timestamp, which JavaScript has support for) and then re-calculate the difference between local time and the saved time when the page loads. There are actually such handly libraries that help out with time operations, such as Moment.js.
I would recommend using LocalStorage over cookies, cookies are also sent to the server, and are designed more for authentication/session tokens. Here’s a good article to get started:
You can use for example localstorage to save timer there and then just to use it.
Please try below example:
// Set an item to localstorage
localStorage.setItem('name', 'value');
// Read the item from localstorage
localStorage.getItem('name'); => // => 'value'
I am using a countdown timer I’ve found in google, I just modify some line of codes to make the timer to count up instead of counting down. I just want my timer to count up then it stops when I click the stop button. My problem is I want the timer to not reset even if the page is refresh or the browser is restart. From what I know I need to use cookies for this but I don’t know how to incorporate it to my code. Please help..
That is not possible until you make some mechanism so store the lastest time frame in some local or server storage. On local side, you can user
Comment posted by KHS
Hi @NotABot can you help me to incorporate it with my code? I’m not really sure on how to do it or you can just give me a hint on how to do it. Thank you.
Comment posted by developer.mozilla.org/en-US/docs/Web/API/Window/…
See you can find that either user has reload/close window(browser) using
Comment posted by KHS
Thanks for the link @NotABot will read about beforeunload to understand further.
Comment posted by KHS
This also worked too.. but I want my timer to start counting from 00:00:00. Anyway, thanks for sharing your knowledge this will be a great help for me and to others in the future. God bless.
Comment posted by KHS
when i tried the code above it will reset every time I refresh the page.
Comment posted by KHS
It will still reset when I refresh the page.
Comment posted by KHS
Actually, i want my timer to not reset whenever I refresh or restart my browser. Your first answer is what I need the only problem is I want it to start counting up from 00:00:00 then stops when I click the button stop, your answer starts at a given time, I want it from 00:00:00 then stops whenever I click the stops button.
Comment posted by mplungjan
the code I have posted will start from 00:00:00 whenever started from scratch or reloaded after the test of 24 hours expires
Comment posted by Oddrigue
You should elaborate your answer a bit, adding documentation or code snippets and explaining how it would work for others
Comment posted by JS Countdown Using Localstorage Blog
Thank you. Yes, you’re correct. I am providing a blog link as a reference.
Comment posted by KHS
Thank you for your help @Oleh Morozov it worked!!! This literally saves a lot of time. Been working for 2 days for this trying to figure it out. God bless!