Jquery/Ajax update database every x minutes

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Basically i am wanting to update a field every 5 minutes per user.
For example i have 10 users, and a field called count.
Every 5 minutes i want count to be count + 1

I know this can be done via cron in php, however after having a look through stack at a few similar questions, many answers have been 'it can not be done due to cron being server bases and jquery/ajax being clientside'.

Another issue i may face is being 1 user may update every 4.5 minutes or even 4 minutes and the remaining 9 updating every 5.

Is there a way to do this per user using ajax? Along with displaying a countdown timer until their next 'update'

Im not too sure what approach to take, any insight/thoughts would be great
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,457
I wouldn't set the time in jQuery either way, as they can edit it. You could set a time var in the database, and everytime they (re)load a page, it checks how many minutes have gone by after the last update. Make sure you truncate the var in the database after they logout/leave the page, though.
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
I dont think will quite work. As it will still need to work when the user is offline.
As there will be a current count and a max count.
Meaning if the users current count is less than max count. Update it.
Whereas if the users current count is the same value as the max count, dont do anything. As it cant exceed the max count
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
If all you're wanting to do to is have some SQL queries executed every x minutes, then I don't see how a cron would not work.
Because the times will vary, majority will be every 5 minutes. However as I stated, there may be times where other people get an update at different times.
One user may have 4 mins and 30 seconds, another may have 4 mins and 10 seconds.

A prime example would the likes of mafia/mob wars on facebook/mobile.
Energy, health, stamina get updated every x minutes, however the times may differ based upon the users chosen starting class.
When the user is online it updates the value there and then (update via ajax)
Whereas if the user is offline, they still get the same update.
Obviously once they reach the 'max allocated amount' (100/100 or 50/50), the update gives that user nothing, whereas other users will remain to get updated
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,396
960
Because the times will vary, majority will be every 5 minutes. However as I stated, there may be times where other people get an update at different times.
One user may have 4 mins and 30 seconds, another may have 4 mins and 10 seconds.

A prime example would the likes of mafia/mob wars on facebook/mobile.
Energy, health, stamina get updated every x minutes, however the times may differ based upon the users chosen starting class.
When the user is online it updates the value there and then (update via ajax)
Whereas if the user is offline, they still get the same update.
Obviously once they reach the 'max allocated amount' (100/100 or 50/50), the update gives that user nothing, whereas other users will remain to get updated
Sounds like you'd just have a cron for each class then
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
I suppose, but I was hoping to have an option where users could decrease the 'update time' whether it be completing a certain task, or reaching a certain level. Something like minus 10 seconds off their current time. Which is why I question
Would this then mean I would need to allocate a value for each user to determine which cron they run off?
And then have multiple crons which differ in a 10 seconds de-increments from the previous?

eg. range is 5 mins (base), with the potential to get down to 3 min 30.
Meaning this would require me to have 10 crons that start from 5mins and lower them in increments of 10.
That being said, some will be redundant, up until a person reaches the threshold to advance their timer so it is faster (which is a timeframe I can not determine as it is based on the users gameplay speed)

Would that not be wasting resources?
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,396
960
It wouldn't be a waste of resources, but you may want to think out the logistics of how you want to design the database. Based on what you've said, it seems like count should be accompanied by a timestamp. When a user completes a task that would decrease the update time, you update the timestamp. Then the cron can just check against timestamp and see which users need their count increased.
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Do you only want updates to occur on the users account when they have the client open, or even when they are away?
Whether they are online or not. It needs to run

Basically the user will have energy, max_energy, stamina & max_stamina
If the users current stamina is less than max_stamina, + 1 it when their time is due. If they are equal, do nothing. Same for energy.

However user 1, may have their times as followed: energy, increases every 5 minutes, and stamina increases every 4 mins 20
User 2 however, may have these times: energy, increases every 4 mins 20, and stamina increases every 3 mins 50.
And the remaining users may have energy and stamina both at 5mins due to them being inactive.

Which leads me back to the questions would I need to have multiple crons set for each 'timer'?


Based on what you've said, it seems like count should be accompanied by a timestamp. When a user completes a task that would decrease the update time, you update the timestamp. Then the cron can just check against timestamp and see which users need their count increased.
Would that not mean I would need to have a cron set every second?
Simply due to the fact that not all users will have the same updated timestamp, as updating said field (more info in my response to rasta) would need to happen as and when the time reaches zero, ready to restart its loop from said users timer, rather than updating when a check is in place as it would not be instant
 

RastaLulz

fight teh power
Staff member
May 3, 2010
3,926
3,923
Whether they are online or not. It needs to run

Basically the user will have energy, max_energy, stamina & max_stamina
If the users current stamina is less than max_stamina, + 1 it when their time is due. If they are equal, do nothing. Same for energy.

However user 1, may have their times as followed: energy, increases every 5 minutes, and stamina increases every 4 mins 20
User 2 however, may have these times: energy, increases every 4 mins 20, and stamina increases every 3 mins 50.
And the remaining users may have energy and stamina both at 5mins due to them being inactive?
So to be clear, you want still want a user's energy/stamina to increase if they don't even have the game open? If not, you could have the timer(s) client side for these things, and simply make an AJAX request when you want to update something in the database. Obviously the script on the server side would have to verify the time difference.
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
So to be clear, you want still want a user's energy/stamina to increase if they don't even have the game open? If not, you could have the timer(s) client side for these things, and simply make an AJAX request when you want to update something in the database. Obviously the script on the server side would have to verify the time difference.
Yes exactly that.
So when the game is open once their countdown reaches zero, the said stat will increase by 1 and their timer will reset back to whatever time they are on (whether it be 5mins, 4mins 30 or even 3mins 50) and restart the process (obviously if their current stat is equal to their max_stat there will be no timer/no action taken)
Whereas if they are offline, they would not see the timer and/or the increase of 1, until they load up the game after 'x' time and see they have got 'x' stats over the time they have been offline.

I may have to just forget the various times, and stick with one set time to keep it simplistic
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,457
You could just do this in 1 cronjob that runs every 30 seconds for example. Just let it check everything, and a last_updated timestamp to calculate the time difference, and store in the database how many seconds the difference has to be to check.
 

Users who are viewing this thread

Top