How can I put a three second delay within an if loop?

Hey there! I know there have been questions very similar to this using millis() before but I can’t find a way to build those answers into my program. Timer is a variable for something else. I would like to add a three second delay after displaying the text before the program moves on to something else. This is within a draw loop.

if (response>-1){
if (response==rightans){
timer=-1;
fill(255);
textSize(80);
text(“Correct!”,175,260);
//(three second delay goes here)
}
if (response!=rightans){
timer=-1;
fill(255);
textSize(80);
text(“Wrong”,200,260);
//(three second delay goes here)
}

Thanks for the response in advance

Just name it differently

such as timerForInitialText

When the time has passed set a boolean showInitialText to false (initially before setup () set it to true obviously)

In draw() act on this boolean variable: if it’s true show text else go on to main part

Hey There!

By pause the program you mean stop it ? The delay() exists.

https://processing.org/reference/delay_.html

And to stop it from halting the whole program you could look at threading()

https://processing.org/reference/thread_.html

1 Like

Hi,

the millis() function is the way to go.

The idea is to save the time at which you want to start waiting (imagine starting a chronometer).
Then, you check the time over and over until the new time is passed the waiting time that you wanted.

You can do that like so:

long waitStart = millis();
while (millis() - waitStart < 3000) {
   // Do nothing
}
// Do something after the 3 seconds

Now, putting a 3 seconds wait inside an if statement (or your draw() function for that matter) is probably not the best way to go since you’ll freeze your whole application during that time and it won’t be responsive at all…

You probably want to split your application into different states and use a combination of flags and timer to get the job done.

To illustrate, consider the following exemple:
I want to draw a rectangle on the screen when I click the mouse button for 3 seconds then get rid of it. Your first go might be to do something like this:

boolean showRect = false;
long startWait;

void setup() {
  size(800, 600);
  background(20);
}

void draw() {
  background(20);

  if (showRect) {
    startWait = millis(); // I start the timer since I just start to draw the rectangle
    fill(200, 0, 0);
    rect(300, 200, 200, 200);
  }

  fill(200);
  ellipse(mouseX, mouseY, 50, 50);

  // I have drawn the rectangle and I'm waiting three seconds 
  while (millis() - startWait < 3000) {
    // Do nothing
  }
  showRect = false;
}

void mouseClicked() {
  showRect = true;
}

You can try, but you won’t even see the rectangle. The reason is that all the elements are beeing drawn only when the draw loop is finished. In this case when you clic, it waits 3 seconds BEFORE finishing it so it is drawing the rectangle only for one frame.

Instead, you should not block the main loop and simply check the time everytime you go trhough that loop like so:

boolean showRect = false;
long startWait;

void setup() {
  size(800, 600);
  background(20);
}

void draw() {
  background(20);
  
  if (millis() - startWait > 3000) {
    showRect = false;
  }
  
  if (showRect) {
    fill(200, 0, 0);
    rect(300, 200, 200, 200);
  }
  
  fill(200);
  ellipse(mouseX, mouseY, 50, 50);
}

void mouseClicked() {
  showRect = true;
  startWait = millis();
}
1 Like

here is my version

int timerForInitialText; // timer 
boolean showInitialText=true;  // the program has 2 states 

void setup() {
  size(700, 700);
  background(0);

  timerForInitialText=millis();
}

void draw() {
  //
  background(0); 

  // the program has 2 states:  
  if (showInitialText) {
    // state 1: show initial text for 3 seconds
    text ("Intro", 111, 111);
    if (millis()-timerForInitialText >= 3000) { 
      showInitialText=false;
    }
  } else {
    // state 2: normal program / game (not ending)
    text ("normal", 111, 111);
  }
}//
//
1 Like