Share via


Small Basic: Timer

This article is about the Timer object in the Microsoft Small Basic programming language.

In this article:


What is a Timer

Timer is an object that generates timer events in a specified interval.

Timer Object

The Timer object has 1 property, 2 operations and 1 event.

Properties

The Timer has only one property - Interval.  The default Interval is 100000000 [ms] (27.7 [h]).

  • Interval - can be set to or gotten for the duration (ms) for timer events.

Events

Timer has one event.

  • Tick - to set timer event handler.

Operations

The Timer object has the following two operations.

  • Pause - pauses timer event.
  • Resume - resumes timer event.

Event Handler

A timer event handler is a subroutine that is called when the interval has expired. There are two types to write event handlers.  The first one does the target procedure in the event handler.

Timer.Interval = 1000
Timer.Tick = OnTick
Sub OnTick
  Timer.Pause()
  DoSomething()
  Timer.Resume()
EndSub
Sub DoSomething
  Turtle.Move(10)
EndSub

The second one does the target procedure out of the event handler.

Timer.Interval = 1000
Timer.Tick = OnTick
While "True"
  If tick Then
    DoSomething()
    tick = "False"
  Else
    Program.Delay(300)
  EndIf
EndWhile
Sub OnTick
  tick = "True"
EndSub
Sub DoSomething
  Turtle.Move(10)
EndSub

Sample Programs

The following are sample programs showing the use the Timer object.

  • Stop Watch (QDR256)
  • Kitchen Timer (GQF135) - only for 3 minutes

Known Issues

There are two known issues about the Timer.

Error (Delay)

The Timer has about a 10ms error (delay).  The error of the Timer can be easily confirmed with the Clock.ElapsedMillisecond property.  A sample program has been published as ZHB659.

Short Interval is Heavy in Remote

Short Timer.Interval (such as 20) is too heavy in remote (in a browser with Silverlight).


See Also

Other Resources