# Making a for loop with timer

**URL:** <https://discourse.processing.org/t/making-a-for-loop-with-timer/17085>\
**Category:** Coding Questions\
**Created:** [January 14, 2020, 5:50pm UTC](https://discourse.processing.org/t/making-a-for-loop-with-timer/17085 "2020-01-14T17:50:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Salttt](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@Salttt](https://discourse.processing.org/u/Salttt)\
**Post date:** [January 14, 2020, 5:50pm UTC](https://discourse.processing.org/t/making-a-for-loop-with-timer/17085/1 "2020-01-14T17:50:39Z")

</div>

```auto
int delay = 2000;

void setup() {
  size(200, 200);
  time = millis();
  fill(#989696);
  stroke(#747373);
  rectMode(CENTER);
  rect(width - 70, 35, 120, 40);
}

void draw() {
  for (int i = width - 130; i < width - 10; i += 1) {
    if (millis() - time >= delay) {
      stroke(#00B4A7);
      line(i, 25, i, 55);
      println("n");
      time = millis();
    }
  }
} 

```

I am trying to essentially draw a line every two seconds, and then moving that line over one pixel and repeating. Any help would be appreciated 🙂

---

<div class="post-metadata">

**Author:** ![everythingability](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/everythingability/32/937_2.png) [@everythingability](https://discourse.processing.org/u/everythingability)\
**Post date:** [January 14, 2020, 6:06pm UTC](https://discourse.processing.org/t/making-a-for-loop-with-timer/17085/2 "2020-01-14T18:06:16Z")

</div>

Like this… (Cheers Dan!)

```auto
int delay = 2000;
Timer timer; 
int i;

void setup() {
  size(400, 200);
  timer = new Timer(1000); // Create a timer that goes off every 1 second
  timer.start();
  fill(#989696);
  stroke(#747373);
  rectMode(CENTER);
  rect(width - 70, 35, 120, 40);
  i = width - 130;
  
}

void draw() {
  if (timer.isFinished()){
      stroke(#00B4A7);
      line(i, 25, i, 55);
      println("Timer", i);
       i++;
      timer.start();
    }
       
  if (i > width - 10){
    i = width - 130; //reset to your starting point
  }else{
   
  }
}

// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 10-4: The raindrop catching game

class Timer {

  int savedTime; // When Timer started
  int totalTime; // How long Timer should last

  Timer(int tempTotalTime) {
    totalTime = tempTotalTime;
  }

  void setTime(int t) {
    totalTime = t;
  }

  // Starting the timer
  void start() {
    // When the timer starts it stores the current time in milliseconds.
    savedTime = millis();
  }

  // The function isFinished() returns true if 5,000 ms have passed. 
  // The work of the timer is farmed out to this method.
  boolean isFinished() { 
    // Check how much time has passed
    int passedTime = millis()- savedTime;
    if (passedTime > totalTime) {
      return true;
    } else {
      return false;
    }
  }
}

```
