Home > Software engineering >  P5.JS - Action every few seconds
P5.JS - Action every few seconds

Time:01-18

I am quite new to p5 and would need some advice.

Is it possible to trigger an action only every few seconds? For example: After two seconds i ;

If I use draw() or an own loop function it just goes way to fast.

Thanks a lot. Max

CodePudding user response:

you can make use of setInterval(function, delay) as shown below:

let counter = 0;

function increment() {
  counter  ;
}

function setup() {
  createCanvas(400, 400);
  setInterval(increment, 2000); // 2000 ms = every 2 seconds
  fill(0);
}

function draw() {
  background(220);
  text(counter, 100, 100);
}

I have set up an Example Sketch for you here.

You can read more about setInterval on MDN

  •  Tags:  
  • Related