# Printing delay of 500 millisecs between indices of a for loop iteration

**URL:** <https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201>\
**Category:** Beginners\
**Tags:** homework\
**Created:** [January 19, 2021, 3:56pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201 "2021-01-19T15:56:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![decentralizedmike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/decentralizedmike/32/11122_2.png) [@decentralizedmike](https://discourse.processing.org/u/decentralizedmike)\
**Post date:** [January 19, 2021, 3:56pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201/1 "2021-01-19T15:56:43Z")

</div>

Hi p5 pals!

I am having trouble trying to print out text with a delay between each iteration of the for loop.  
Instead of printing all at once in realtime, I would like a 1/2 sec delay between each of the indices.

I have tried using setInterval() for this but It repeats the for loop as well as the function that is being called inside setInterval() parameter.

Any other ways ya recommend at approaching this?

btw the p. before p5 library functions is because i am using react to embed p5 within my project.

```auto
let queue = [] // this queue has strings of data from an api. I want to print each index.

function printHeadlines() {
      p.fill(255);
      p.textSize(30);
      p.text('title', x, y );

      for (let i = 0; i < 10; i++) {
        p.textSize(14);
        x = x + 30;
        setInterval(function(){
        line = p.text([i+ 1] + ". " + queue[i], x, y);
        },500)
      }
    }

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 19, 2021, 4:11pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201/2 "2021-01-19T16:11:06Z")

</div>

a processing example:

```auto
int retriger=0, timer = 500;
void setup() {}
void draw() {
  if(millis() > retriger) {
    retriger = millis()+timer;
    println(millis());
  }
}

```

---

<div class="post-metadata">

**Author:** ![decentralizedmike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/decentralizedmike/32/11122_2.png) [@decentralizedmike](https://discourse.processing.org/u/decentralizedmike)\
**Post date:** [January 19, 2021, 4:43pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201/3 "2021-01-19T16:43:55Z")

</div>

Anyway you can explain how this will look within a for loop?

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 19, 2021, 4:52pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201/4 "2021-01-19T16:52:25Z")

</div>

I forgot the for loop thing. You can use `thread` function to do that. It is just not too efficient.

I haven’t tested it yet but : P

I will post the code in a minute

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 19, 2021, 4:57pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201/5 "2021-01-19T16:57:23Z")

</div>

here it is

```auto
int delay = 500, rep = -1;
void setup() {thread("timer");}
void draw() {background(frameCount % 255);}

void timer() {
  for(int i = 0; i < rep || rep == -1; i++) {
    println(i,millis());
    delay(delay);
  }
}

```

`int delay` sets the interval between reactivations  
`int rep` tells how many times it is repeated (if you set it to 3, it will print: 0, 0, \n 1,500 \n 2,1000), if set to -1, it will repeat infinetely

---

<div class="post-metadata">

**Author:** ![decentralizedmike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/decentralizedmike/32/11122_2.png) [@decentralizedmike](https://discourse.processing.org/u/decentralizedmike)\
**Post date:** [January 19, 2021, 5:00pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201/6 "2021-01-19T17:00:25Z")

</div>

Thanks for your time & input 🙂  
Going to study these pieces of code, and play around with it.  
Bless CodeMasterX !

---

<div class="post-metadata">

**Author:** ![Sierra\_Alpha\_Romeo.P](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sierra_alpha_romeo.p/32/11571_2.png) [@Sierra\_Alpha\_Romeo.P](https://discourse.processing.org/u/Sierra_Alpha_Romeo.P)\
**Post date:** [January 19, 2021, 5:11pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201/7 "2021-01-19T17:11:33Z")

</div>

Hey @decentralizedmike  
I’ve tried something similar. Do you want something like this?

https://editor.p5js.org/Sierra_Alpha_Romeo.P/embed/FNZR5wII3

---

<div class="post-metadata">

**Author:** ![decentralizedmike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/decentralizedmike/32/11122_2.png) [@decentralizedmike](https://discourse.processing.org/u/decentralizedmike)\
**Post date:** [January 19, 2021, 5:11pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201/8 "2021-01-19T17:11:54Z")

</div>

YES YES YES! Exactly

---

<div class="post-metadata">

**Author:** ![Sierra\_Alpha\_Romeo.P](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sierra_alpha_romeo.p/32/11571_2.png) [@Sierra\_Alpha\_Romeo.P](https://discourse.processing.org/u/Sierra_Alpha_Romeo.P)\
**Post date:** [January 19, 2021, 5:13pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201/9 "2021-01-19T17:13:04Z")

</div>

Check out the code  
[https://editor.p5js.org/Sierra\_Alpha\_Romeo.P/sketches/FNZR5wII3](https://editor.p5js.org/Sierra_Alpha_Romeo.P/sketches/FNZR5wII3)  
You need not require the for loop if you are using the setInterval function

---

<div class="post-metadata">

**Author:** ![decentralizedmike](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/decentralizedmike/32/11122_2.png) [@decentralizedmike](https://discourse.processing.org/u/decentralizedmike)\
**Post date:** [January 19, 2021, 5:15pm UTC](https://discourse.processing.org/t/printing-delay-of-500-millisecs-between-indices-of-a-for-loop-iteration/27201/10 "2021-01-19T17:15:18Z")

</div>

Thank You Sierra!  
Yes that makes sense, I was definitely over complicating things.
