Answered by:
Timer

Question
-
I wanted to learn how a Timer works, so I went to the instructional PDF. I did a search for "Timer" and nothing showed up. Then I went to the website with the powerpoint slides and tried to find an entry.
Then I searched the forums but only found specific questions. Where is the general guide to using this one-out-of-a-dozen features of the language.
Saturday, June 18, 2011 5:50 PM
Answers
-
If you haven't seen anything in the places you looked then I don't think there is any formal documentation - this forum is the best source for help beyond the basic introduction.
The Timer has 4 functions:
Timer.Interval sets the interval at which the timer event is called, the input is in ms (milliseconds)
Timer.Tick registers the timer event to a subroutine that is called every time the tick interval is reached
Timer.Pause and Timer.Resume should be fairly self explanatory
Very basic example that does a click every second, and can toggle pause/resumed with a mouse click.
Timer.Interval = 1000
Timer.Tick = OnTick
GraphicsWindow.MouseDown = OnMouseDown
timerOn = "True"
Sub OnTick
Sound.PlayClick()
EndSub
Sub OnMouseDown
If (timerOn) Then
Timer.Pause()
timerOn = "False"
Else
Timer.Resume()
timerOn = "True"
EndIf
EndSub
Saturday, June 18, 2011 8:37 PM
All replies
-
If you haven't seen anything in the places you looked then I don't think there is any formal documentation - this forum is the best source for help beyond the basic introduction.
The Timer has 4 functions:
Timer.Interval sets the interval at which the timer event is called, the input is in ms (milliseconds)
Timer.Tick registers the timer event to a subroutine that is called every time the tick interval is reached
Timer.Pause and Timer.Resume should be fairly self explanatory
Very basic example that does a click every second, and can toggle pause/resumed with a mouse click.
Timer.Interval = 1000
Timer.Tick = OnTick
GraphicsWindow.MouseDown = OnMouseDown
timerOn = "True"
Sub OnTick
Sound.PlayClick()
EndSub
Sub OnMouseDown
If (timerOn) Then
Timer.Pause()
timerOn = "False"
Else
Timer.Resume()
timerOn = "True"
EndIf
EndSub
Saturday, June 18, 2011 8:37 PM -
I added some Timer code to my prog and immediately received some errors upon running that I didn't understand. The second time I ran, without changing the code, I didn't receive any errors. However the program now has a delay in the background (the usual black bacground with the end program button). It takes longer to start up.
Once, I changed a timer variable and received the original error again. And then again when I ran the code a second time (and all subsequent times) the program worked fine but with the aforementioned delay. Here's the nature of what I added:
~body~
WaitInterval = 100
WaitSub()
~body~
Sub
WaitSubTimer.Interval = 10
TickTotal = 0
Wait:
Timer.Tick = OnTick
If TickTotal < WaitInterval Then
Goto Wait
EndIf
EndSub
Sub
OnTickTickTotal = TickTotal + 1
EndSub
Monday, June 20, 2011 7:59 PM -
Based on the code you posted, with some comments and a diagnostic (debug output):
WaitInterval = 100 'Create a varaible OK
WaitSub() 'Call a subroutine OK
Sub WaitSub
'initialise some variables and start the timer event
Timer.Interval = 10
TickTotal = 0
Wait:
TextWindow.WriteLine(TickTotal) ' To see when this is called and TickTotal value
Timer.Tick = OnTick
'The timer is started (or restarted) and is called every 10 ms
'TotalTick is incremented and the subroutine (and program) ends when TickTotal = 100 (100 ticks and 1 sec total)
If TickTotal < WaitInterval Then
Goto Wait ' This goes to Wait and restarts the timer - not a good idea to repeatedly restart the Timer, but seems to accept it although I wouldn't be sure the intervals would be right if it is continually restarted
EndIf
EndSub
Sub OnTick
TickTotal = TickTotal + 1
EndSub
Based on what it looks like you are trying to do (1000 ms delay), perhaps all you want is:
Program.Delay(1000)
To help further, would need more details of what your objectives are and a full sample code (simplified as far as possible to show how you are approaching the objective.
Good luck.
Monday, June 20, 2011 9:00 PM -
Both relocating the Timer.Tick statement and/or using Program.Delay seem to work without error. There is still a delay in the black background that used to pop up immediately upon executing the program, but I guess I'll chalk that up to the use of delay in any form.Tuesday, June 21, 2011 12:21 PM
-
I am also trying to figure out timer, and i saw in the above post that there was a 'Goto wait' and basically i am trying to make it where 30 sec after my program is ran, it will Goto 'somewhere' Could you please help me figure out what code to use? i am unfamiliar with 'timer' thankyou.Wednesday, August 3, 2011 4:45 AM
-
If you don't want your program to do anything while you are waiting you could just do Program.Delay(30000).
If you want you program to be doing something during the wait, you could set the set the timer interval to 30 sec, then when it is first called set a flag that you capture and go somewhere. The following is just an idea - exactly what is the best way will depend on the program and what is happening during the wait.
Timer.Interval = 30000 'Timer interval 30 sec Timer.Tick = OnTick 'Start Timer intervalDone = 0 'Flag that 30 sec is not up While ("True") ' You program can do stuff here TextWindow.WriteLine("We are waiting") If (intervalDone = 1) Then Goto NEXT EndIf Program.Delay(1000) ' Just to slow things EndWhile NEXT: TextWindow.WriteLine("The waiting is finished") Sub OnTick intervalDone = 1 'Set flag that 30 sec is up Timer.Pause() 'Stop timer 'WE CANNOT DO A GOTO JUMP OUT OF A SUBROUTINE SO WE MUST SET A FLAG AND CHECK IT ELSEWHERE EndSub
Wednesday, August 3, 2011 5:25 PM