CodingTrain / Suggestion-Box

A repo to track ideas for topics
571 stars 86 forks source link

setTimeout() in draw() #1367

Open doktorbanana opened 5 years ago

doktorbanana commented 5 years ago

hi,

i'm new to coding and have some problems understanding the setTimeout() and setInterval() functions. I want to use them to draw something every 2seconds. How do i do that?

I tried: `function setup() { createCanvas(400, 400); }

function draw() { background(220); setInterval(doit,2000) }

function doit(){ ellipse(200,200,20); }`

That doesnt work. I guess that has to do with the loop of the draw-function. Can someone help?

Thanks.

AnthonyTonev commented 5 years ago

draw() is a interval by itself - by default it will draw a thing in each 1/60 of a second. You can modify that in setup with - frameRate(10) - will draw a thing each 1/10 of a second. What can stop the loop with noLoop() but in this case just don't use draw(). Just place the interval outside and place the background(220) function in doit(). Try this:

function setup() { createCanvas(400, 400); }

setInterval(doit,2000);

function doit(){ background(220); ellipse(random(0,200),random(0,200),20); }

На чт, 1.08.2019 г. в 11:28 ч. doktorbanana notifications@github.com написа:

hi,

i'm new to coding and have some problems understanding the setTimeout() and setInterval() functions. I want to use them to draw something every 2seconds. How do i do that?

I tried: `function setup() { createCanvas(400, 400); }

function draw() { background(220); setInterval(doit,2000) }

function doit(){ ellipse(200,200,20); }`

That doesnt work. I guess that has to do with the loop of the draw-function. Can someone help?

Thanks.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/CodingTrain/Rainbow-Topics/issues/1367?email_source=notifications&email_token=AH24R64AAHQROJ27W25ETADQCKNEJA5CNFSM4IIOETE2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HCYIM3Q, or mute the thread https://github.com/notifications/unsubscribe-auth/AH24R6YSLEF7WHD5CE2MVLDQCKNEJANCNFSM4IIOETEQ .

doktorbanana commented 5 years ago

Ahh great. Thanks.