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?