-2

Sorry if this is a basic question, but I want to have a function trigger every fixed amount of time, say 2 seconds. In general I would do something like:

lifetime = getCurrentTime
if (lifetime - lastTime > triggerTime)
    doTheThing()
    lastTime = lifetime
end

This normally works fine, but I'm working on a program where each object stores its own lifetime, and there could be hundreds of objects at once. I'm wondering if there's a way that each object wouldn't need to also remember its own lastTime -- I was thinking something with rounding or modulo, but even then you'd still need to remember how many times the function had triggered before.

I'm wondering if anyone knows of a good general way to have a trigger function every x seconds without having to remember the last time it triggered or how many times it's triggered?

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
Adam L.
  • 91
  • 5
  • Every x seconds, trigger all of them, and then you only store the time once. – user253751 Jan 19 '23 at 19:42
  • The most common thing here would be to use a standard feature already given by your Framework or System like a Timer or a Scheduler. – Ralf Jan 19 '23 at 19:45
  • 1
    For your specific example of "every two seconds", you can do `if seconds % 2 == 0`, which does not require any sort of memory. This will work for any target interval that evenly divides into 60. – John Gordon Jan 19 '23 at 19:45
  • Or with a bit more calculation, you can get seconds-past-midnight which would be good enough for almost any use case. – John Gordon Jan 19 '23 at 21:00
  • It is unclear if you question is for lua or python you should tag to languages "just becuase" – Nifim Jan 19 '23 at 23:28

1 Answers1

0

Have you tried using sched yet? Here's a task done every 5 seconds to get you started:

import sched
import time                                                                                                                                           
                                                                                                                                                             
def doTheThing(runnable_task, every):                                                                                                                        
    runnable_task.enter(every, 1, doTheThing, (runnable_task, every))                                                                                        
    print("The thing...")                                                                                                                                    
                                                                                                                                                             
                                                                                                                                                             
task = sched.scheduler(time.time, time.sleep)                                                                                                                
                                                                                                                                                             
every = 5                                                                                                                                                    
task.enter(every, 1, doTheThing, (task, every))                                                                                                              
                                                                                                                                                             
task.run() 
Axe319
  • 4,255
  • 3
  • 15
  • 31
JustLearning
  • 1,435
  • 1
  • 1
  • 9