xxxxxxxxxx
41
let delaytime, thunder;
function setup() {
createCanvas(800, 600);
background(000);
noLoop();
thunder = createAudio('thunder.mp3');
button = createButton('click me');
button.position(0, 0);
button.mousePressed(doAsync);
// doAsync();
}
async function doAsync() {
delaytime = random(2000) + 2000;
background(000);
await sleep(delaytime);
background(255);
await sleep(100);
background(000);
delaytime = random(1000) + 500
await sleep(delaytime);
thunder.play()
// uncomment to see a second flash at thunder time
//background(255);
//await sleep(100);
//background(000);
await sleep(10000)
thunder.stop()
background(222);
textSize(32);
textAlign(CENTER);
text('Time: ' + int(delaytime) + ' ms', 400, 300);
}
function sleep(millisecondsDuration)
{
return new Promise((resolve) => {
setTimeout(resolve, millisecondsDuration);
})
}