# Help with delay/millis without influencing the framerate

**URL:** https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153
**Category:** Coding Questions
**Created:** [February 6, 2019, 12:19pm UTC](https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153 "2019-02-06T12:19:25Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)
#### Post date: [February 6, 2019, 12:19pm UTC](https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153/1 "2019-02-06T12:19:25Z")

</div>

Hi i am trying to make some text pop up each letter at a time by using a string while my framerate is at 60 however i can not use delay() because it would influence the framerate. How can i do this without using delay?

p.s i have tried to use millis() but i can not find an example i can use

---

<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: [February 6, 2019, 12:22pm UTC](https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153/2 "2019-02-06T12:22:30Z")

</div>

yes, use millis() to make a timer

> [@The screen is updating after while loop is completed ? need help](https://discourse.processing.org/t/the-screen-is-updating-after-while-loop-is-completed-need-help/7119/3):
>
> here a example using a millis timer: long startT, stopT=1000; float ang = 0; float colc; color rf; void setup() { size(500, 500); colc=random(255); rf=color(colc, 0, 255-colc); } void myTimer() { if ( millis() \> startT + stopT ) { startT = millis(); println("\_next loop\_"); // what else you need to be done now? ang++; colc=random(255); rf=color(colc, 0, 255-colc); } } void draw() { background(255, 244, 206); myTimer(); translate(width/2, height/2); …

---

<div class="post-metadata">

### Author: ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)
#### Post date: [February 6, 2019, 12:32pm UTC](https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153/3 "2019-02-06T12:32:16Z")

</div>

Thank you for the code. I will see if i can use it

---

<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: [February 6, 2019, 3:09pm UTC](https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153/4 "2019-02-06T15:09:59Z")

</div>

i just made a delux version:  
revision acc next post @neilcsmith

```auto
// timer variables
long startT, stopT=5000;
boolean diagT = true;
int typT = 1; // 0 OFF, 1 ONESHOT, 2 CONTI

void setup() {
  //size(500, 500);
  println("operation:\nrestart oneshot timer key [t]\nconti timer key [c]\ntimer OFF key [q]\ndelta key [+] or [-]\nprinting key [p]");
  theTime("first start");
}

void theTime(String info) {
  if ( diagT ) println(info+"_time_"+year()+"-"+month()+"-"+day()+" "+hour()+":"+minute()+":"+second());
}

void myTimer() {
  boolean doit = false;
  if ( millis() > startT + stopT ) {
    if ( typT == 1 ) { // ONE SHOT
      theTime("_oneshot");
      doit = true;
      typT = 0; // timer OFF
    } else if ( typT == 2) { // CONTI
      // https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153/5
      // startT = millis(); // auto restart
      startT += stopT;
      theTime("_conti");
      doit = true;
    }
    if ( doit ) {
      if ( diagT ) println("+ + what else you need me to do now?");
      // ____________________________________________ your code
    }
  }
}

void draw() {
  background(200,200,0);  
  myTimer();
}

void keyPressed() {
  if ( key == 't' ) { // manual restart ONESHOT timer
    typT = 1;
    startT = millis();
    theTime("_start");
  }
  if ( key == 'c' ) { // manual change to CONTI timer
    typT = 2;
    startT = millis();
    theTime("_start");
  }
  if ( key == 'q' ) { // manual change to timer OFF
    typT = 0;
    if ( diagT ) println("timer OFF");
  }
  if ( key == 'p' ) { // enable disable diag printing
    diagT = ! diagT;
    println("printing "+diagT);
  }
  if ( key == '+' ) { // increase
    if ( stopT < 1000 ) stopT += 100;
    else if ( stopT >= 1000 ) stopT += 1000;
    println("set "+stopT);
  }
  if ( key == '-' ) { // decrease
    if ( stopT > 1000 ) stopT -= 1000;
    else if ( stopT <= 1000 ) stopT -= 100;
    else if ( stopT < 0 ) stopT = 10;    
    println("set "+stopT);
  }
}

```

and also a “nano” version:

```auto
// v01 https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153
// v02 change from 
// startT = millis(); // auto restart
// to: startT += stopT;
// thanks to @neilcsmith
// https://discourse.processing.org/t/inaccurate-time-intervals-for-a-metronome/2463/10
// v03 also change use nano instead millis

// timer variables
long startT=0, msec = 1000000L, stopT=(long) 5000*msec; // 5 sec
boolean diagT = true;
int typT = 2; // 0 OFF, 1 ONESHOT, 2 CONTI
// user variables
int usercounter = 0;

void setup() {
  size(200, 200);
  frameRate(120);
  println("operation:\nrestart oneshot timer key [t]\nconti timer key [c]\ntimer OFF key [q]\ndelta key [+] or [-]\nprinting key [p]");
  startT = System.nanoTime();
  theTime("first start");
  fill(0,0,200);
}

void theTime(String info) {
  if ( diagT ) println(info+"_time_"+year()+"-"+month()+"-"+day()+" "+hour()+":"+minute()+":"+second());
}

void myTimer() {
  boolean doit = false;
// if ( millis() > startT + stopT ) {

  if ( System.nanoTime() > startT + stopT ) {
    if ( typT == 1 ) { // ONE SHOT
      theTime("_oneshot");
      doit = true;
      typT = 0; // timer OFF
    } else if ( typT == 2) { // CONTI
      // https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153/5
      // startT = millis(); // auto restart
      startT += stopT;
      theTime("_conti");
      doit = true;
    }
    if ( doit ) {
      // println("+ + what else you need me to do now?");
      // ____________________________________________ your code
      usercounter++;
    }
  }
}

void draw() {
  background(200,200,0);  
  myTimer();
  if ( diagT ) text(nf(frameRate,1,1)+" FPS; dT "+stopT/msec+" msec; T: "+typT,5,height-10); //
  if ( diagT ) text(usercounter,width/2,height/2);   
}

void keyPressed() {
  if ( key == 't' ) { // manual restart ONESHOT timer
    typT = 1;
    startT = System.nanoTime();
    theTime("_start");
  }
  if ( key == 'c' ) { // manual change to CONTI timer
    typT = 2;
    startT = System.nanoTime();
    theTime("_start");
  }
  if ( key == 'q' ) { // manual change to timer OFF
    typT = 0;
    if ( diagT ) println("timer OFF");
  }
  if ( key == 'p' ) { // enable disable diag printing
    diagT = ! diagT;
    println("printing "+diagT);
  }
  if ( key == '+' ) { // increase
    if ( stopT < 1000*msec ) stopT += 100*msec;
    else if ( stopT >= 1000*msec ) stopT += 1000*msec;
    println("set "+stopT/msec);
  }
  if ( key == '-' ) { // decrease
    if ( stopT > 1000*msec ) stopT -= 1000*msec;
    else if ( stopT <= 1000*msec ) stopT -= 100*msec;
    if ( stopT < 0 ) stopT = 0;    
    println("set "+stopT/msec);
  }
}

```

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [February 6, 2019, 3:54pm UTC](https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153/5 "2019-02-06T15:54:32Z")

</div>

You might also want to have a look at [Inaccurate time intervals for a Metronome](https://discourse.processing.org/t/inaccurate-time-intervals-for-a-metronome/2463/10?u=neilcsmith)

The code looks good apart from the auto restart with `startT = millis()` - this will accumulate errors, although may be what you want - fixed rate or fixed delay?

---

<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: [February 6, 2019, 4:30pm UTC](https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153/6 "2019-02-06T16:30:37Z")

</div>

and just i looked up your

```auto
System.nanoTime()

```

> <https://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime>

  
is actually not very clear for me.

i come from the arduino side and have made some ?multi tasking? time scheduler? structures there  
and learned also that the call “millis” takes too long,  
so the basic loop i just made only by counters ( revolving job1=1001; job2=1002;…)  
and used millis and check serial as rare as possible.

for this timing critical things in processing i not yet have a feeling, even i work mostly  
on my 40euro computer Raspberry PI with 1GB ram for cpu+gpu  
so i should learn the hard way.

ok: as long have println’s in draw and not use noSmooth(); in setup,  
i think it is all not that critical.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [February 6, 2019, 4:45pm UTC](https://discourse.processing.org/t/help-with-delay-millis-without-influencing-the-framerate/8153/7 "2019-02-06T16:45:07Z")

</div>

> [@kll](#):
>
> and learned also that the call “millis” takes too long,

I prefer using nanoTime() as it’s a more accurate clock. On Windows for example the millis() clock can have a coarse resolution (only changes value every 16ms). The call to nanoTime() is computationally more expensive, but on a desktop computer (including the Pi!) it’s really not worth being concerned about compared to everything else Processing is doing.
