In my app I want to show countdown to my users. I know there are lot of extensions to create countdown using date. But I want to show same countdown for all my users.
I need a 30 second countdown. Also I want to run the countdown in a loop so that when the countdown gets over, it will start 30 second countdown again. But I want to show this countdown same for all the users in a sync. Like if there are 20 seconds left, then it should show same on all devices for all users. So what I did is I added 30 seconds to the current timestamp in milliseconds and stored that timestamp in my database and when the value us fetched from database, I have created a countdown between two timestamps (one is the current timestamp) and other is the value which is stored in my database which is future timestamp (+30 seconds).
Now the problem is when the countdown reaches 0 or when the countdown is finished, I need to again add 30 seconds to the current timestamp and update the value in my database to make this countdown run in a loop forever.
But this is something which I canât code inside my application because it will only trigger when the app is running or after a specific action is performed by user and it wonât work properly because I want to run the countdown continuously on a loop and I want it to be synchronised for all users so that all users will see same time.
So how do I update the timestamp value automatically in my database every 30 seconds without user interaction so that the countdown will work in a loop?
No need of any database for this. The simple trick, in my point of view is, just get online time from web, trim only seconds and use if else then condition as below
Afte getting time enable clock to timer,(time interval must set to 1000 and untick both options from designer part)
Catch the trimmed seconds in a variable(say global secds)
When clock timers
If the online secds is less than 30 then subtract trimed secd from 30 else subtract it from 60âĶ
No, I can fetch current time from web, that is not the problem.
I need to add 30 seconds to that timestamp and I will need to store that value in my database because storing in local database like TinyDB will not work because I want to show same countdown for all users. My question is how do I automatically update the timestamp value in my database (for example: Firebase)?
The current timestamp is: 1643648732263 (milliseconds)
Now I want to add 30 seconds in this timestamp which is 30,000 milliseconds. So it becomes 1643648732263 + 30,000 = 1643648762263
So this is the timestamp which I will store in my database and in my app, I am using extension to show countdown which takes two parameters, start date and end date. So the start date will be the current timestamp which I will fetch from the web and end date will be the value which I stored in my database so it will give me the countdown.
Now my question is: When the countdown is finished, I again want to do this same process. I want to again update the timestamp value by adding more 30 seconds in the current timestamp. But I canât keep doing this all day obviously because no one has this much of free time. I want this to be done automatically.
I thought of creating a separate application for this which will keep updating the timestamp every 30 seconds in my database, but againâĶIt is not possible for me to keep application running 24/7 just to make the countdown work in a loop.
I hope you understood what I am trying to achieveâĶ Do you have any solution for this?
Edit: Please have a look at this post. This is exactly what I need: