# How to create a simple timer?

**URL:** https://discourse.processing.org/t/how-to-create-a-simple-timer/3901
**Category:** Coding Questions
**Created:** [September 27, 2018, 1:52pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901 "2018-09-27T13:52:53Z")
**Posts on this page:** 17
**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: [September 27, 2018, 1:52pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/1 "2018-09-27T13:52:53Z")

</div>

Hello,  
I think this is a rather simple question, but how do I create a timer(or something like that), that tells the programm to do thing 1, and then, after half a second, do thing 2? I tried using the frameRate() command(do you call stuff like that commands i don’t know), but it did not really work out.

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [September 27, 2018, 2:03pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/2 "2018-09-27T14:03:03Z")

</div>

Hi Schred,

Check out the [millis()](https://processing.org/reference/millis_.html) function.

You can find plenty of example on the forum on how to use it 🙂

---

<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: [September 27, 2018, 2:05pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/3 "2018-09-27T14:05:29Z")

</div>

Thanks, I already have a little experience with that, but I heared that it’s “exhausting” for the processor, to run this command that often. Is there an easier way? (Or is that simply not true?)

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [September 27, 2018, 2:11pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/4 "2018-09-27T14:11:23Z")

</div>

I can’t say for sure but I don’t think it is heavy on the processor and if you really just want to make a simple timer, it will definitly not be too much for the processor to handle.

---

<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: [September 27, 2018, 2:12pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/5 "2018-09-27T14:12:01Z")

</div>

Okay thanks, I’ll try it then! 😄

---

<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: [September 27, 2018, 4:03pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/6 "2018-09-27T16:03:49Z")

</div>

> [@Array and object moving and not moving Coding Questions](https://discourse.processing.org/t/array-and-object-moving-and-not-moving-coding-questions/698/2):
>
> Here’s an example which displays a blue background() for 5 seconds, followed by a red background() for 2 seconds, relying on a class library I coded called “Countdown.java”: sunglasses[https://Forum.Processing.org/two/discussion/27733/countdown-class-library-for-java-js-python](https://Forum.Processing.org/two/discussion/27733/countdown-class-library-for-java-js-python) /\*\* \* Countdown Pause/Resume Example (v1.0) \* GoToLoop (2018/Jun/04) \* \* https://Discourse.Processing.org/t/ \* array-and-object-moving-and-not-moving-coding-questions/698/2 \*/ import gotoloop.countdown.Countdow…

---

<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: [September 27, 2018, 4:22pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/7 "2018-09-27T16:22:01Z")

</div>

Thanks dude, I’ll take a look at it 😄

---

<div class="post-metadata">

### Author: ![ScottGrogin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scottgrogin/32/1689_2.png) [@ScottGrogin](https://discourse.processing.org/u/ScottGrogin)
#### Post date: [September 27, 2018, 4:26pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/8 "2018-09-27T16:26:32Z")

</div>

For something super simple you could just increment an int. If your sketch is running at 30 fps then every 30 increments is 1 second.

---

<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: [September 27, 2018, 4:31pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/9 "2018-09-27T16:31:25Z")

</div>

@ScottGrogin Well I tried that before with “something = 1/frameRate;” and testing for a value that should only appear once during the 60 frames but NOPE it did not work out (with “if(blabla == something)”) … XD 😉

---

<div class="post-metadata">

### Author: ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)
#### Post date: [September 27, 2018, 4:37pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/10 "2018-09-27T16:37:54Z")

</div>

Be careful when checking for true equality when trying to time something. It will almost never match the exact time you want.

The best you can do is to check if the elapsed time is higher than the time you wanted and then perform the action you want in this case.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [September 27, 2018, 4:40pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/11 "2018-09-27T16:40:02Z")

</div>

Libraries also includes TimedEvents, Timing Utilities, and Countdown Timer.

> **[Libraries](https://processing.org/reference/libraries/)**
>
> Extend Processing beyond graphics and images into audio, video, and communication with other devices.

`==` will not work, because the precise millisecond a frame occurs on is not reliable.

```auto
void draw(){
  print(millis(), ' ');
}

```

Run it three times:

> 513 531 546 562 579 595 615 629 648 662 681 695 713 729 746 764 782 795

> 533 550 566 585 600 617 632 651 666 700 719 734 750 767 785 801 817 835

> 485 504 522 537 554 571 585 602 619 639 652 672 686 704 719 737 751 772

…it will print different values every time you run it. Use `>`.

---

<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: [September 27, 2018, 4:40pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/12 "2018-09-27T16:40:44Z")

</div>

@jb4x I might try that next time! 😄 (For now, I’ve got a solution :P)

---

<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: [September 27, 2018, 4:43pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/13 "2018-09-27T16:43:13Z")

</div>

@jeremydouglass That’s what kept me from doing it that way, I just didn’t know how to fix it. 😄 Next time I’ll try it 🙂

---

<div class="post-metadata">

### Author: ![Aitesedba](https://avatars.discourse-cdn.com/v4/letter/a/3be4f8/32.png) [@Aitesedba](https://discourse.processing.org/u/Aitesedba)
#### Post date: [November 22, 2020, 2:33pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/14 "2020-11-22T14:33:05Z")

</div>

` text ((frameCount/frameRate),width/2,height/2);`  
Displays the exact amount of time the program has been running for

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [November 22, 2020, 2:41pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/15 "2020-11-22T14:41:49Z")

</div>

or use

` text (millis(),width/2,height/2);`

---

<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: [November 23, 2020, 7:20am UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/16 "2020-11-23T07:20:59Z")

</div>

Haha, thanks for your replies. I’m done with that project by now though. ;D

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [September 3, 2021, 2:46pm UTC](https://discourse.processing.org/t/how-to-create-a-simple-timer/3901/17 "2021-09-03T14:46:17Z")

</div>


