How to wait a couple seconds in code.

Like the title says I would just like to wait one or two seconds before executing another line of code.

@brendanballon Codea isn’t designed to wait. It is constantly running in a loop executing the code. Your question doesn’t give enough information for why you want to wait. If you have some code you don’t want to execute for a couple of seconds, that’s easy to do. But to just wait a couple of seconds is interrupting the Codea execution cycle. Can you give more info on why or what you want to wait.

I want to wait a few seconds before showing some text.

@brendanballon There’s different and more elegant ways to do what you want, but here’s a simple example. The draw function gets executed 60 times per second, so a variable can be incremented and an if statement used to show various text delays.

PS. It’s hard to give you a useful example without knowing anything about how you’re going to display your text.

function setup()
    timer=0
    fill(255)
end

function draw()
    background(40, 40, 50)
    if startTimer then
        timer=timer+1
    end
    text("Tap screen to show text at varying delays",WIDTH/2,HEIGHT/2)
    if timer>60*2 then
        text("waited 2 seconds",WIDTH/2,HEIGHT-50)   
    end
    if timer>60*4 then
        text("waited 4 seconds",WIDTH/2,HEIGHT-100)   
    end
    if timer>60*8 then
        text("waited 8 seconds",WIDTH/2,HEIGHT-150)   
    end
    if timer>60*12 then
        text("waited 12 seconds",WIDTH/2,HEIGHT-200)   
    end
end

function touched(t)
    if t.state==BEGAN then
        startTimer=true
    end
end

That is exactly what I needed! Thank

I have a question. If I place a “heavy load” on this program will that effect the timing of things or is 60 times per second a hard and fast rule. Just curious.

@Scotty The heavier the load, the slower it goes. You can create a large for loop in the draw function that completely stops it. Using a timer like I show above isn’t the correct way to do it. That was just a quick example.

Thanks. @dave1707

@Scotty Here’s an example of slowing down draw() . Tap the screen to double the for loop limit. It takes a high count before you start to notice a slow down.

function setup()
    limit=1
    fill(255)
    val=0
end

function draw() 
    background(40, 40, 50)    
    for z=1,limit do        
    end
    val=val+1
    text("for loop limit  "..limit,WIDTH/2,HEIGHT/2-100)
    str=string.format("running draw() every %2.4f  seconds",DeltaTime)
    text(str,WIDTH/2,HEIGHT/2)
    text("tap screen to double for loop limit",WIDTH/2,HEIGHT/2-150)
end

function touched(t)
    if t.state==BEGAN then
        limit=limit*2
    end
end