xxxxxxxxxx
43
let x = 0;
let count = 0;
let timerP;
let interval = {
isActive: false,
id: -1,
start(callback){
this.id = setInterval(callback, 500);
callback();
this.isActive = true;
},
stop(){
clearInterval(this.id);
this.id = -1;
this.isActive = false;
}
};
function setup() {
createCanvas(200, 200);
timerP = select('.timer');
timerP.parent(createDiv());
}
function mousePressed(){
if(interval.isActive) {
interval.stop();
} else {
interval.start(function(){
timerP.html(count++);
});
}
}
function draw() {
background(0);
stroke(255);
line(x, 0, x, height);
x += 3;
if (x > width) {
x = 0;
}
}