# Timers

A Timer can be used to generate a periodic event. You could use this to implement a background save capability or to issue time-out warnings or maybe just to build your own custom digital clock. To create a timer use this code:
Copy

    constant TIMER = createTimer() -- TIMER is now the ID of the new timer

To start a timer operating use:
Copy

        startTimer(TimerID, OwnerID, n)
        -- TimerID is the ID of the timer
        -- OwnerID is the ID of the window that WM_TIMER messages will be sent to
        -- n is the number of milliseconds between each timer event.

To stop a timer use:
Copy

    stopTimer(TimerID)

Later on you can restart the timer by invoking startTimer() again and you can use a different timing value or destination window if you wish. The timing events are sent to the associated window's handler as a WM_TIMER message.
Please note that if any WM_TIMER messages are still in the message queue at the time that stopTimer() is invoked then those queued messages will still be sent to the handler. Because of program latency timing messages will never be accurate. Also, the user will not be able to time very fine events because of the Windows-pegged event resolution. Additionally, Windows only releases WM_TIMER messages when the message queue buffer is otherwise empty so a very "busy" window may miss some timer events when a fresh timer rollover occurs but the previous timer event is still in the message queue. I don't know how many timers are allowed to exist at any one time but I think the fewer there are the better.