# Timer in Python mode

**URL:** https://discourse.processing.org/t/timer-in-python-mode/29286
**Category:** Processing.py
**Created:** [April 11, 2021, 8:52pm UTC](https://discourse.processing.org/t/timer-in-python-mode/29286 "2021-04-11T20:52:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nktlst](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nktlst](https://discourse.processing.org/u/nktlst)
#### Post date: [April 11, 2021, 8:52pm UTC](https://discourse.processing.org/t/timer-in-python-mode/29286/1 "2021-04-11T20:52:00Z")

</div>

I need timer, which resets when I press keyboard key (for example). I did basic timer, but it not resets, I understand why but I can’t figure it out how to make my class resets on key board hit. For example I need “stop” function for timer and “reset” function.

Here is my code

```auto
bg = 0
ts = 28 # text size
start = False

def setup():
    size(700,700)
    textSize(ts)
    # noLoop()

def draw():
    background(bg)
    fill(255)
    
    global bg
    global timer
    global timeleft
    
    timeleft = 10 # 10 seconds
    if (start == True):
        timeleft = 10
    if (start == False):
        timeleft = 0
    timer = Timer(timeleft)
    text(timer.getRemainingTime(), 20, 20)

    hit = "Hit SPACEBAR"
    hitw = textWidth(hit)
    text(hit,(width-hitw)/2,(height-ts)/2)
    print(start)
    print(timer)
    
class Timer: # Timer class
    def __init__ (self, duration):
        self.duration = duration
    def getRemainingTime(self):
        return max(0, self.duration - millis()/1000)
            
def keyPressed(): # Press spacebar class + switch on/off
    global bg
    global start
    if (key == ' '): # space
        bg = int(random(100))
        if start == True:
            start = False
        else:
            start = True
            
            

```

---

<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: [April 11, 2021, 8:56pm UTC](https://discourse.processing.org/t/timer-in-python-mode/29286/2 "2021-04-11T20:56:35Z")

</div>

[forum.Processing.org/two/discussion/27733/countdown-class-library-for-java-js-python#Item\_2](http://forum.Processing.org/two/discussion/27733/countdown-class-library-for-java-js-python#Item_2)

---

<div class="post-metadata">

### Author: ![nktlst](https://avatars.discourse-cdn.com/v4/letter/n/9dc877/32.png) [@nktlst](https://discourse.processing.org/u/nktlst)
#### Post date: [April 12, 2021, 8:50pm UTC](https://discourse.processing.org/t/timer-in-python-mode/29286/3 "2021-04-12T20:50:21Z")

</div>

@GoToLoop can you please explain why I need external library?

---

<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: [April 12, 2021, 8:59pm UTC](https://discourse.processing.org/t/timer-in-python-mode/29286/4 "2021-04-12T20:59:07Z")

</div>

Calling the class Countdown a “library” is perhaps a little too overrated. 🤑

It’s merely a wrapper for the already existing builtin class Timer. ⌚

You can pick the idea and do the same for your own code if you prefer. 💡
