notagodzilla
notagodzilla•10mo ago

Programmatically run code every 3 seconds

started programatically
17 Replies
ballingt
ballingt•10mo ago
You probably want to schedule a function to run later, see https://docs.convex.dev/scheduling/scheduled-functions
Scheduled Functions | Convex Developer Hub
Convex allows you to schedule functions to run in the future. This allows you to
notagodzilla
notagodzillaOP•10mo ago
im just updating a timer across clients. im unsure how this would help
ballingt
ballingt•10mo ago
Help me understand what's confusing here, it sounds like you'd like to run some code in the future, in 3 seconds? and then again 3 seconds after that?
notagodzilla
notagodzillaOP•10mo ago
its basically a counter. the one we have in games where it shows the time left it updates every second if every second wont work, i can do3s
ballingt
ballingt•10mo ago
If games are short you can use a long-running action and use timers like you're doing here, just don't return from the function (await a promise) until you're done. But if games can go for more than a few minutes, schedule a function to run in a second. Then that function you schedule can schedule itself again
notagodzilla
notagodzillaOP•10mo ago
is it feasible to schedule one everysecond? i wanted to try cron. bt cron is very project specific than feature specific. i.e i cant deploy cron based on condition, it would be running all the time and inside i should be having my conditions
ballingt
ballingt•10mo ago
Yep, if you use a cron job you'd need to find all the games that need to be updated and update them all in that function yes it's feasible, if you have issues please let us know
notagodzilla
notagodzillaOP•10mo ago
yeah doing that everysecond is very 😬 awesome then ill try that
ballingt
ballingt•10mo ago
Could you say more about 😬
notagodzilla
notagodzillaOP•10mo ago
i dont think if thats good practice to query and mutate everysecond and across games
ballingt
ballingt•10mo ago
what are your concerns here? Running a cron job once a second and scheduling funtions to run once a second are similar, it depends on how many concurrent games you have for which is more efficient (if you have 100 games, running a single cron job function once a second sounds better — if you usualy don't have any games, and occasionally have one, then scheduling update functions sounds better)
notagodzilla
notagodzillaOP•10mo ago
wow i thought having 100 games and running every second was bad will try cron then. thanks @Tom
notagodzilla
notagodzillaOP•10mo ago
can we start a cron programatically? in the example i can see that we can add arguments for it but they are hardcoded https://docs.convex.dev/scheduling/cron-jobs
Cron Jobs | Convex Developer Hub
Convex allows you to schedule functions to run on a recurring basis. For
ballingt
ballingt•10mo ago
You'd write a cron job that once a second queries for all active games and updates them how this scales depends on the size of your games if you want to do this programatically instead you want to use scheduled functions
notagodzilla
notagodzillaOP•10mo ago
yeah got that. was just curious if we can pass args to it since i saw this in the example
No description
ballingt
ballingt•10mo ago
Yep these are hardcoded, you can pass args but it's always the same args
notagodzilla
notagodzillaOP•10mo ago
ok got it thx

Did you find this page helpful?