-- Timer
function setup()
t=00
s=0
m=0
textAlign(CENTER)
fontSize(50)
end
function draw()
background(0)
t=t+1
if t == 60 then
t=00
s=s+1
print(s)
end
if s==60 then
s=00
m=m+1
print(m)
end
text(t..":"..s..":"..m,WIDTH/2,HEIGHT/2)
text(string.format("%02d:%02d:%02d",t,s,m),WIDTH/2,HEIGHT/2-50)
end
function setup()
t=00
s=0
m=0
p=0
textAlign(CENTER)
fontSize(50)
end
function touched(touch)
if touch.state == BEGAN then
if p==0 then
p=1
elseif p==1 then
p=0
end
end
end
function draw()
background(0)
if p==0 then
t=t+1
if t == 60 then
t=00
s=s+1
print(s.."s")
end
if s==60 then
s=00
m=m+1
print(m.."m")
end
end
text(string.format("%02d:%02d:%02d",t,s,m),WIDTH/2,HEIGHT/2)
if p==1 then
text("PAUSED",WIDTH/2,HEIGHT/2 - 50)
end
end ```
function setup()
t,s,m=0,0,0
fontSize(50)
end
function touched(touch)
if touch.state == BEGAN then
p = not p -- toggle p between true or false
end
end
function draw()
background(0)
if p then
text("PAUSED",WIDTH/2,HEIGHT/2 - 50)
else
t=t+1
if t == 60 then
t=0
s=s+1
if s==60 then
s=0
m=m+1
end
end
end
text(string.format("%02d:%02d:%02d",t,s,m),WIDTH/2,HEIGHT/2)
end
The issue with creating a timer by incrementing in the draw function is that there’s no guarantee this will actually run 60 times per second. Heavy processing could cause the frame rate to drop and the timer would then be inaccurate.
I’d suggest using Jarc’s suggestion or the ‘ElapsedTime’ variable to make sure this is actually accurate.
Splitting the timer into seconds & minutes also complicates the loop needlessly. It would be better (and easier) to just use seconds and calculate the duration when initialising the timer (seconds + minutes * 60).
@Steppers ElapsedTime is also affected by heavy processing. Using tween delay doesn’t give you the ability to pause/show the time. It’s mainly used for process delays. If you want a timer unaffected by heavy processing then use the socket function gettime. Under heavy processing you won’t get a smooth timer display because the draw function will be slowed down, but the time values will be correct.
Here’s an example using the socket function gettime.
viewer.mode=FULLSCREEN
function setup()
font("Courier")
s=require("socket")
h,m,sec,dd=0,0,0,0
st=0
end
function draw()
background(0)
fill(255)
format()
fontSize(100)
text(string.format("%02d:%02d:%02d.%02d",h,m,sec,dd),WIDTH/2,HEIGHT/2)
fontSize(30)
text("Tap screen to start/pause/lap",WIDTH/2,HEIGHT-100)
end
function touched(t)
if t.state==BEGAN then
if st==0 then
st=s:gettime()
end
run=not run
end
end
function format()
if run then
diff=s:gettime()-st
h=diff//3600
m=(diff%3600)//60
sec=(diff%60)//1
dd=((diff*100)%100)//1
end
end
@RonJeffries Any timer that adds 1 per draw cycle and divides by 60 to get seconds will be wrong on your iPad. But the above code that uses socket gettime will still be correct on yours.
@RonJeffries I believe ElapsedTime would work on the 120. As I mentioned above, under a heavy load, ElapsedTime loses time. So it’s not as accurate under all circumstances. Under a normal load, ElapsedTime works well.