# Text() and Delay()

**URL:** <https://discourse.processing.org/t/text-and-delay/5698>\
**Category:** Coding Questions\
**Created:** [November 19, 2018, 2:10am UTC](https://discourse.processing.org/t/text-and-delay/5698 "2018-11-19T02:10:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMonkey](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemonkey/32/2416_2.png) [@CodeMonkey](https://discourse.processing.org/u/CodeMonkey)\
**Post date:** [November 19, 2018, 2:10am UTC](https://discourse.processing.org/t/text-and-delay/5698/1 "2018-11-19T02:10:20Z")

</div>

I want text to appear on the screen, but letter by letter and slowly.  
When I execute this code nothing is displayed to the user and I’m unsure as to why.

> String textLine = “This is a test to see if I can do this”;  
> String output=“”;  
> boolean continueRunning = true;

> void setup() {  
> size(1240, 980);  
> }

> void draw() {  
> while(continueRunning ==true){  
> for(int i = 0;i\<textLine.length();i++){  
> output+=textLine.charAt(i);  
> text(output,100,400);  
> delay(1000);  
> }  
> continueRunning=false;  
> }  
> }

-My thought process was to see if I could manage to loop through textLine, pull the letter from that index and add it onto the end of the output, then delay and repeat with the next index.  
-I figured by doing this output should eventually be equal to textLine and be displayed with Text() through each iteration of the loop.  
-The boolean continueRunning is just so the loop happens once.

---

<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:** [November 19, 2018, 2:39am UTC](https://discourse.processing.org/t/text-and-delay/5698/2 "2018-11-19T02:39:44Z")

</div>

- pls format your code with

> \</\>

- how you start coding?  
you complain you see NOTHING  
ok, i test a reduction of your code and also  
see a big empty window.

```auto
//String textLine = "This is a test to see if I can do this";
String output="test";
//boolean continueRunning = true;

void setup() {
  size(1240, 980);
}

void draw() {
// while (continueRunning ==true) {
// for (int i = 0; i<textLine.length(); i++) {

// output+=textLine.charAt(i);

      text(output, 100, 400);
// delay(1000);
// }
// continueRunning=false;
// }
}

```

possibly you work on this until you see some text

later we do the next step.

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 19, 2018, 3:16am UTC](https://discourse.processing.org/t/text-and-delay/5698/3 "2018-11-19T03:16:39Z")

</div>

Don’t use `delay()`. It is not the function you want. It does not do what you think it does. It is not useful in this situation. Do not use `delay()`. Do not use `delay()`. Do not use `delay()`. Do not use `delay()`. Do not use `delay()`.

Do not use `delay()`.

DO NOT-

You get the point.

Use `millis()`!

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 19, 2018, 3:21am UTC](https://discourse.processing.org/t/text-and-delay/5698/4 "2018-11-19T03:21:51Z")

</div>

```auto
int time;
int n;

void setup() {
  size(400, 400);
  time = millis() + 1500;
  n = 1;
}

void draw() {
  background(0);
  for ( int i = 0; i < n; i++) {
    ellipse(15 + 30 * i, 15, 20, 20);
  }
  if ( millis() > time ) {
    n++;
    time = millis() + 1500;
  }
  if ( n > 10 ) {
    n = 1;
  }
}

```

Imagine what you could do with the `n` variable, your String, and the substring() function…

---

<div class="post-metadata">

**Author:** ![CodeMonkey](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemonkey/32/2416_2.png) [@CodeMonkey](https://discourse.processing.org/u/CodeMonkey)\
**Post date:** [November 19, 2018, 4:42am UTC](https://discourse.processing.org/t/text-and-delay/5698/5 "2018-11-19T04:42:26Z")

</div>

I do not have the same issue you have. If I run the code with the reductions you had the word “test” appears.

---

<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:** [November 19, 2018, 4:58am UTC](https://discourse.processing.org/t/text-and-delay/5698/6 "2018-11-19T04:58:43Z")

</div>

very good, now start with a timer

```auto

String textLine = "This is a test to see if I can do this ";
String output = "";
int i = 0;
// use basic millis timer
long startT, actionT=500, nowT;

// _______________________________________________
void my_timer() {
  nowT = millis();
  if ( nowT > startT + actionT ) {
    startT = nowT;
    do_something();
  }
}

// _______________________________________________
void do_something() {
  output += textLine.charAt(i);
  println("i: "+i+" char: "+output);
  i++; 
  if ( i >= textLine.length() ) { 
    i = 0; 
    output = "";
  }
}

// _______________________________________________
void setup() {
  // size(1240, 980);
  size(300, 100);
  startT = millis();
}

// _______________________________________________
void draw() {
  background(200, 200, 0);
  //stroke(0, 0, 200);
  fill(0, 0, 200);
  text(output, 10, 50);
  my_timer();
}

```

pls. note the print line,

```auto
println("i: "+i+" char: "+output);

```

this is the basic way to do diagnostic, so you know where you are and what is happening

---

<div class="post-metadata">

**Author:** ![CodeMonkey](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemonkey/32/2416_2.png) [@CodeMonkey](https://discourse.processing.org/u/CodeMonkey)\
**Post date:** [November 19, 2018, 5:09am UTC](https://discourse.processing.org/t/text-and-delay/5698/7 "2018-11-19T05:09:28Z")

</div>

Thank you so much to the both of you for your help. Do you think you could explain why delay() wasn’t what I wanted?  
[https://processing.org/reference/delay\_.html](https://processing.org/reference/delay_.html)

From it’s description it sounds as if it just stops the program for a little while.

Once again though, thank you so much. millis() is what I needed.

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 19, 2018, 5:23am UTC](https://discourse.processing.org/t/text-and-delay/5698/8 "2018-11-19T05:23:22Z")

</div>

Imagine there is a robot painter. He puts some paint on a canvas and shows it to you. It is a nice picture. But the robot is a very fast painter. It immediately stops showing you the picture and paints another one. And it keeps doing this.

If the picture he painted was the same each time, it would look like it was just a painting. But if the robotic painter painted a slightly different picture each time, it would look like an ANIMATED painting.

The robotic painter is `draw()`. It draws a picture, and then when it is done, it shows you the resulting image for one frame.

When you call delay, you are not saying “Stop drawing pictures for a while and let me keep looking at the last one.” NO! You are telling the robot painter to take a break in the middle of making a painting. You don’t get to see any new frames until his break is over!

So really, what delay says is “Stop drawing paintings for some amount of time AND ALSO STOP SHOWING ME THEM.” So not only does the program stop drawing, it ALSO stops updating what you are seeing! Yikes! This is NOT what you want. Delay is NOT doing what you thought it would do!

Using `millis()` is like having the robotic painter look at a ticking clock, and using the current time to know what to paint. It’s a much better way of drawing things based on elapsed time.

---

<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:** [November 19, 2018, 11:17am UTC](https://discourse.processing.org/t/text-and-delay/5698/9 "2018-11-19T11:17:28Z")

</div>

> [@TfGuy44](#):
>
> substring() function

that actually would be the shortest version:

```auto

String textLine = "This is a test to see if I can do this ";
int i = 0;
long startT, actionT=500, nowT; // use basic millis timer
// _______________________________________________
void my_timer() {
  nowT = millis();
  if ( nowT > startT + actionT ) {
    startT = nowT;
    i++; 
    if ( i >= textLine.length() ) i = 0;
  }
}
// _______________________________________________
void setup() {
  size(300, 100);
  fill(0);
}
// _______________________________________________
void draw() {
  background(255);
  text(textLine.substring(0, i), 10, 50);
  my_timer();
}

```
