0

I would like if it is possible to have different delay times for different texts in Processing. I would like them to update on different times instead of simultaneously.

Qiu
  • 5,651
  • 10
  • 49
  • 56
nmn
  • 11
  • 1

1 Answers1

1

You an use two variables (one to keep track of time and one to store the delay amount) for a basic independent delay system. Have a look at this post post for more details. If you need multiple delays, you would have multiple pairs of these variables. For a basic setup it may work, but if you have a lot of delays, it can get pretty messy, pretty fast.

If it helps, I started putting together a basic setup so you can run delays a-la javascript's setTimeout() and setInterval(): the idea is you call setTimeout if you want a single function call after a certain delay, passing the name of your function as a string and the delay in millieconds:

color firstColor,secondColor,thirdColor;
float rectWidth;
void setup(){
  size(400,400);
  noStroke();
  rectWidth = width / 3.0;

  setTimeout("onFirstDelay",1000);
  setTimeout("onSecondDelay",1500);
  setTimeout("onThirdDelay",1750);
}
void draw(){
  fill(firstColor);
  rect(0,0,rectWidth,height);

  fill(secondColor);
  rect(rectWidth,0,rectWidth,height);

  fill(thirdColor);
  rect(rectWidth*2,0,rectWidth,height);
}

void onFirstDelay(){
  firstColor = color(192,0,0);
}
void onSecondDelay(){
  secondColor = color(0,192,0);
}
void onThirdDelay(){
  thirdColor = color(0,0,192);
}

//this code can be in a separate tab to keep things somewhat tidy
void setTimeout(String name,long time){
  new TimeoutThread(this,name,time,false);
}
void setInterval(String name,long time){
  intervals.put(name,new TimeoutThread(this,name,time,true));
}
void clearInterval(String name){
  TimeoutThread t = intervals.get(name);
  if(t != null){
    t.kill();
    t = null;
    intervals.put(name,null);
  }
}
HashMap<String,TimeoutThread> intervals = new HashMap<String,TimeoutThread>();

import java.lang.reflect.Method;

class TimeoutThread extends Thread{
  Method callback;
  long now,timeout;
  Object parent;
  boolean running;
  boolean loop;

  TimeoutThread(Object parent,String callbackName,long time,boolean repeat){
    this.parent = parent; 
    try{
      callback = parent.getClass().getMethod(callbackName);
    }catch(Exception e){
      e.printStackTrace();
    }
    if(callback != null){
      timeout = time;
      now = System.currentTimeMillis();
      running = true;  
      loop = repeat; 
      new Thread(this).start();
    }
  }

  public void run(){
    while(running){
      if(System.currentTimeMillis() - now >= timeout){
        try{
          callback.invoke(parent);
        }catch(Exception e){
          e.printStackTrace();
        }
        if(loop){
          now = System.currentTimeMillis();
        }else running = false;
      }
    }
  }
  void kill(){
    running = false;
  }

}

Notice the 3 different rectangles changing colours at different delays.

Community
  • 1
  • 1
George Profenza
  • 50,687
  • 19
  • 144
  • 218