# Task repeat every 10 seconds

**URL:** <https://discourse.processing.org/t/task-repeat-every-10-seconds/18028>\
**Category:** Coding Questions\
**Created:** [February 22, 2020, 9:21pm UTC](https://discourse.processing.org/t/task-repeat-every-10-seconds/18028 "2020-02-22T21:21:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathan1](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@nathan1](https://discourse.processing.org/u/nathan1)\
**Post date:** [February 22, 2020, 9:21pm UTC](https://discourse.processing.org/t/task-repeat-every-10-seconds/18028/1 "2020-02-22T21:21:26Z")

</div>

Hi, I am using p5 js and I am trying to have some code execute every 10 seconds. I am having trouble finding the solution. Thanks.

---

<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:** [February 22, 2020, 9:37pm UTC](https://discourse.processing.org/t/task-repeat-every-10-seconds/18028/2 "2020-02-22T21:37:30Z")

</div>

Hello,

Explore using:  
[https://p5js.org/reference/#/p5/millis](https://p5js.org/reference/#/p5/millis)

`:)`

---

<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:** [February 22, 2020, 10:13pm UTC](https://discourse.processing.org/t/task-repeat-every-10-seconds/18028/3 "2020-02-22T22:13:07Z")

</div>

> **[WindowOrWorkerGlobalScope.setInterval()](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval)**
>
> The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call.

---

<div class="post-metadata">

**Author:** ![nathan1](https://avatars.discourse-cdn.com/v4/letter/n/8baadc/32.png) [@nathan1](https://discourse.processing.org/u/nathan1)\
**Post date:** [February 23, 2020, 1:22pm UTC](https://discourse.processing.org/t/task-repeat-every-10-seconds/18028/4 "2020-02-23T13:22:11Z")

</div>

I just realized that what I meant was to have a 10 second wait then have code executing for ten seconds, then not execute for 10 seconds and repeat.

---

<div class="post-metadata">

**Author:** ![iamjustcurious](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/iamjustcurious/32/8016_2.png) [@iamjustcurious](https://discourse.processing.org/u/iamjustcurious)\
**Post date:** [February 23, 2020, 2:30pm UTC](https://discourse.processing.org/t/task-repeat-every-10-seconds/18028/5 "2020-02-23T14:30:10Z")

</div>

You can use the library called “ **Timing Utilities** ”.

 ![Untitled](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2eb22901ab3298034e0c742795ca212bf83c63b3.png)

---

<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:** [February 23, 2020, 6:43pm UTC](https://discourse.processing.org/t/task-repeat-every-10-seconds/18028/6 "2020-02-23T18:43:43Z")

</div>

Hello,

Ideally you should make a flowchart.

To get you started:

- What time is it at start? Do this in setup()  
timeLast = millis()

- What time is it now? You are now in draw()  
timeNow = millis()

- What is elapsed time?  
timeElapsed = timeNow - timeLast

- Is elapsedTime 10s or more?  
Do something!  
and reset timer to start over when this condition is met  
timeLast = millis()

- Rinse and repeat

`:)`

---

<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:** [February 23, 2020, 7:24pm UTC](https://discourse.processing.org/t/task-repeat-every-10-seconds/18028/7 "2020-02-23T19:24:31Z")

</div>

> [@glv](#):
>
> Is elapsedTime 10s?

is elapsedTime \>= 10s (= 10000 ms)
