Hello , I am trying to understand the concept of timers in p5js. If i understand , there is no way to initialize two timers in the draw loop. I was using the millis function to take the note of time. For example in the snippet below. I have initialized another timer , timer2 inside the if condition . But when I console log it , it has the same value as for timer1. Is there a way to start a new clock from 0 in the if block ?
var xPos = 0;
let timer1 ;
let timer2 ;
function setup() {
createCanvas(400, 400);
noStroke();
}
function draw() {
background(220);
timer1 = int(millis()/1000);
console.log(timer1);
fill(255,200,40);
rect(xPos, 180, 40,40);
xPos = xPos + 1;
if(xPos > width/2)
{
timer2 = int(millis()/1000);
console.log("the new timer");
console.log(timer2);
fill('blue');
rect(100,100,200,200);
}
}
@glv I think I understood the idea behind the millis function. As you can see in the example code. However , as mentioned in the question, I was expecting a different time stamp once inside the if statement above . But when i console log it i get the same time stamp output . That is why i was confused .
Hi, My aim is to calculate two timers . So for example timer 1 starts as soon as millis() is called . If we just for example say after 5 seconds the xPos enters the if block. Then that moment and timer1 is 5 seconds . and timer2 should be 0. so anything beyond that point timer 1 would be 5,6,7,8,9 seconds while timer2 would be 1,2,3,4 seconds
Hi , i think I partly understand this. But I am not sure . What i wanted to achieve is that in the if condition , the timer should reset to 0 and start counting from inside the loop. Is that achieved from the boolean condition ?