# How to make cooldown timer for button

**URL:** <https://discourse.processing.org/t/how-to-make-cooldown-timer-for-button/4890>\
**Category:** Coding Questions\
**Created:** [October 26, 2018, 6:01am UTC](https://discourse.processing.org/t/how-to-make-cooldown-timer-for-button/4890 "2018-10-26T06:01:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![codingsucks](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@codingsucks](https://discourse.processing.org/u/codingsucks)\
**Post date:** [October 26, 2018, 6:01am UTC](https://discourse.processing.org/t/how-to-make-cooldown-timer-for-button/4890/1 "2018-10-26T06:01:06Z")

</div>

I am looking for a way to add a 4 second cool down on using the button ‘w’. How do I achieve this?

---

<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:** [October 26, 2018, 6:55am UTC](https://discourse.processing.org/t/how-to-make-cooldown-timer-for-button/4890/2 "2018-10-26T06:55:06Z")

</div>

You need 3 things:

- A variable to store the state of the button:

- Some sort of a clock to measure the time elapsed after a hit on the button: you can use the `elapsedTime()` function or use a library (I think @GoToLoop has one available)

- An update method that change the state of the button by checking the elapsed time since the last hit and the 4 seconds threshold you want to apply

Without any code we can’t really help you more.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 26, 2018, 2:39pm UTC](https://discourse.processing.org/t/how-to-make-cooldown-timer-for-button/4890/3 "2018-10-26T14:39:16Z")

</div>

button ‘w’ or key ‘w’ ??  
use time from  
millis()  
[https://processing.org/reference/millis\_.html](https://processing.org/reference/millis_.html)  
like

```auto
long nowpress, lastpress, cooldown=4000;

void setup() {
}
void draw() {
}
void keyReleased() {
  if ( key == 'w' ) {
    nowpress = millis();
    if (nowpress > ( lastpress + cooldown )) {
      println("ok, got it");
      lastpress = nowpress;
      // need more of your code what to do at key 'w'
    } else {
      println("not now");
    }
  }
}

```

---

<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:** [October 30, 2018, 6:36pm UTC](https://discourse.processing.org/t/how-to-make-cooldown-timer-for-button/4890/4 "2018-10-30T18:36:56Z")

</div>

You can also create a general-purpose cool-down timer for any number of buttons by creating a HashMap. The key is the character, the value is the time (millis or frame) in the future when the cooldown expires. When you get a new keypress, check the map.

1. If the key exists and the timer hasn’t expired, ignore it.
2. Else put the key/value and do the keypress.
