# How to create better timers?

**URL:** <https://discourse.processing.org/t/how-to-create-better-timers/7938>\
**Category:** Coding Questions\
**Created:** [January 29, 2019, 6:42pm UTC](https://discourse.processing.org/t/how-to-create-better-timers/7938 "2019-01-29T18:42:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [January 29, 2019, 6:42pm UTC](https://discourse.processing.org/t/how-to-create-better-timers/7938/1 "2019-01-29T18:42:23Z")

</div>

Hi!  
So I have experimented with timers before (on a basic level), but never got anything elegant. Actually I think my approach is pretty messy. I’m working on a small game at the moment, and want to add more objects to an ArrayList over time. This is how I did it:

```auto
void draw(){ // simplification
  void addNewTargetObjects(){
    if(timeSinceStart() >= 5000)
      targetList.add(new Target());
   }
}

```

timeSinceStart() is:

```auto
  float timeSinceStart(){
    float timeSinceStart = dist(millis(), 0, startTime, 0); 
    return timeSinceStart; // float startTime = time the game was started/reset
  }

```

But this means that another object is being added every single frame. That’s a liiiiittle bit to much. I would solve this problem by creating a boolean that stores whether the object was already added or not, but that creates just so many unnecessary variables/aka a big array. I just don’t like that method. Do you have any ideas?

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [January 29, 2019, 6:45pm UTC](https://discourse.processing.org/t/how-to-create-better-timers/7938/2 "2019-01-29T18:45:53Z")

</div>

I guess I could ask if targetList.size() is equal to something in this case…

```auto
if(timerSinceStart() >= 5000 && targetList.size() == 1){
   ...
}

```

…but I’m rather looking for a general answer here.😀

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [January 29, 2019, 7:49pm UTC](https://discourse.processing.org/t/how-to-create-better-timers/7938/3 "2019-01-29T19:49:41Z")

</div>

It sounds like you just need to update the `startTime` variable, or use a `boolean` variable to track whether the event has already happened.

You could get more advanced and encapsulate this logic in a class. Then if you wanted one-time behaviors, you could remove the instance after it fires.

Also note that this line:

```auto
float timeSinceStart = dist(millis(), 0, startTime, 0); 

```

Can just be this:

```auto
float timeSinceStart = millis() - startTime; 

```

---

<div class="post-metadata">

**Author:** ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)\
**Post date:** [January 30, 2019, 2:36pm UTC](https://discourse.processing.org/t/how-to-create-better-timers/7938/4 "2019-01-30T14:36:43Z")

</div>

Thanks, I think I’ll try to create a class for that 😃

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 30, 2019, 3:17pm UTC](https://discourse.processing.org/t/how-to-create-better-timers/7938/5 "2019-01-30T15:17:24Z")

</div>

> [@Schred](#):
>
> Thanks, I think I’ll try to create a class for that 😃

> <https://gist.github.com/GoToLoop/ea4db8a8622e27541213da3c9a43ca21>
