How would I build a 24 hour timer?

Options
Brandon Hassler
Brandon Hassler Member
edited September 2023 in ? Transforming data

What I am Wanting to Accomplish
You know how a lot of apps reward you for logging in and collecting some type of bonus that becomes available every 24 hours? I'm trying to figure out how to build that in Xano/WeWeb.

I'm wanting to gamify my site a little bit that will reward users w/ points for certain actions that then enter them into a monthly giveaway. One of the "actions" would be visiting my site (logged in) every 24 hours.

On a special page I want to have a button that as soon as they press it rewards points and then (ideally) shows a message that says something like "Come back at [DATE & TIME] to claim your next bonus!" where the button will re-enable and allow them to collect.

The Stumbling Block
Clicking the button and causing that API request to add points to an existing value is simple, but the timer is trickier. I'm guessing I would have a separate true/false field that would dictate whether the button would be clickable. So as soon as points are added, the "allow" field would switch from true to false and then…..that's where I could use some vision.

Some Possible Theories
I'm guessing I would have a timestamp field that would stamp when the button was last clicked. But what would be the most efficient way for Xano to know to flip that "allow" value to true after 24 hours?

Running a background task across my database every 10 seconds doesn't seem efficient as the userbase grows. Perhaps when the user's page is loaded on the WeWeb side, an API call is automatically made that runs a check? Essentially the function would look to see if it's been 24 hours since the timestamp and if so, it would update the "allow" field to true?

If that's the case, I suppose my only question is how in Xano can I check to see if it's been a certain amount of time since a timestamp?

Tagged:

Answers

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    I would make a field on your user like "last_played" as a timestamp. I'd also have another table or redis for keeping track of the game on a global basis like "game_start" which indicates when the game starts. A daily process would update those variables to be for today/tomorrow as appropriate.

    When they come to the page, you get a the user's "last_played" and ask whether it's greater than "game_start". If so, show the "come back tomorrow!" where the countdown is based on game_start + 24 hours (or 86,400,000 milliseconds). If not, let them play, and the POST for that should update the "last_played" to now.