xxxxxxxxxx
50
/*
There are different ways to handle the timing of events
One way is to use the JavaScript function setInterval() to repeat a function at every given time-interval measures in milliseconds
1 second == 1000 milliseconds
Coding Train Reference:
https://www.youtube.com/watch?v=CqDqHiamRHA
*/
let state = true;
function setup() {
createCanvas(400, 400);
background(220);
// execute a function every one second
// setInterval() takes 2 arguments:
// 1 - the function to execute
// 2 - the amount of time to wait before
// calling the function again
setInterval(doSomething, 1000);
}
function doSomething() {
console.log("I'm doing something");
// flip a boolean variable
// to the opposite state
state = !state;
}
function draw() {
background(220);
// when the sketch starts
// fill the square yellow
// after one seconds, fill it black
// change the color back after one second
if (state) {
fill(255, 255, 0)
} else {
fill(0)
}
rectMode(CENTER);
square(width/2, height/2, 100)
}