# Need assistance with timing! \[millis()\]

**URL:** <https://discourse.processing.org/t/need-assistance-with-timing-millis/18854>\
**Category:** Coding Questions\
**Created:** [March 20, 2020, 10:57pm UTC](https://discourse.processing.org/t/need-assistance-with-timing-millis/18854 "2020-03-20T22:57:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mythikal](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Mythikal](https://discourse.processing.org/u/Mythikal)\
**Post date:** [March 20, 2020, 10:57pm UTC](https://discourse.processing.org/t/need-assistance-with-timing-millis/18854/1 "2020-03-20T22:57:23Z")

</div>

Hey! Thanks for coming to help me out. Today I’m completely stumped on how to get a timer mechanism to work. I’m making a clicker type game and I cant figure out how to add money every second.

```auto
my goal is this:

for each 1000 milliseconds add x to total

```

I have no idea how to check to see if its been 1000 milliseconds. I’m assuming you have to do some subtraction and stuff but I don’t know what order of operations to use.

i tried assigning millis() to a variable and trying out a few math formulas, but nothing is working out for me. I’m sure the solution is super simple, but I am absolutely stumped. Doesn’t help that all the code on other forum posts makes no sense to me/has no notes lol

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 20, 2020, 11:15pm UTC](https://discourse.processing.org/t/need-assistance-with-timing-millis/18854/2 "2020-03-20T23:15:51Z")

</div>

Hello,

Show us what you have done so far.

---

<div class="post-metadata">

**Author:** ![Mythikal](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Mythikal](https://discourse.processing.org/u/Mythikal)\
**Post date:** [March 20, 2020, 11:53pm UTC](https://discourse.processing.org/t/need-assistance-with-timing-millis/18854/3 "2020-03-20T23:53:00Z")

</div>

I don’t have much for it. I didn’t really know where to even start.

my best guess was this:

```auto
int ms;
float moneyTotal;
float idleMoney = 1;

void draw(){
   CPS();
}
void CPS(){
   ms = millis();
   if(ms >= 1000){
      moneyTotal += idleMoney;
      ms = 0;
   }
}

```

that doesn’t work unfortunately, and I tried a few other things but still to no avail.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 21, 2020, 12:00am UTC](https://discourse.processing.org/t/need-assistance-with-timing-millis/18854/4 "2020-03-21T00:00:16Z")

</div>

I provided a simple example:

```auto
int whattimeisitnow;
int whattimeisitatstartofstopwatch;
int timepassedtoresetstopwatch = 1000;

//setup runs once
void setup()
  {
  whattimeisitatstartofstopwatch = millis();  
  }

//draw loops 60 frames per sec  
void draw()
  {
  whattimeisitnow = millis();
  if (whattimeisitnow > whattimeisitatstartofstopwatch + timepassedtoresetstopwatch)  
    {
    println("1000 ms have passed!");
    whattimeisitatstartofstopwatch = millis(); //reset stopwatch and start over
    }
  }

```

This is a very basic timer that is very common; I just added descriptive variable names.

I would like you to explain to me how it works and then try writing it from scratch.  
I do this often (code from scratch) as an exercise to work my brain.

You can also add println() statements to see what is going on with code.  
Keep in mind that millis() is constantly updating in the background with the passage of time and the variables will update.

[https://processing.org/reference/millis\_.html](https://processing.org/reference/millis_.html)

`:)`

---

<div class="post-metadata">

**Author:** ![Mythikal](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Mythikal](https://discourse.processing.org/u/Mythikal)\
**Post date:** [March 21, 2020, 12:56am UTC](https://discourse.processing.org/t/need-assistance-with-timing-millis/18854/5 "2020-03-21T00:56:54Z")

</div>

this will work perfectly! thanks!
