Timer Functions


  typedef struct Timer  Timer;
  typedef void (*TimerAction)(Timer *t);

  struct Timer {
    App *       app;            /* associated App */
    int         milliseconds;   /* interval between actions */
    int         remaining;      /* time until action */
    TimerAction action;         /* user-defined action */
    void *      data;           /* user-defined data */
    int         value;          /* user-defined value */


  int     delay(App *app, int milliseconds);
  long    current_time(App *app);

  Timer * new_timer(App *app, TimerAction action, int milliseconds);
  void    del_timer(Timer *t);


The delay function suspends program operation for the required number of milliseconds, returning the number of milliseconds elapsed. The return value may be less than the required milliseconds if the delay was interrupted by an external event. The timing of this function is likely to be inaccurate for very small delays, since operating system activity and hardware interrupts are likely to produce small variations in the actual delay time. This function should be used sparingly.

The current_time function returns the approximate time in milliseconds since the program began. This number will overflow if the program is used continuously for more than a month.

A Timer is an object which allows a program to have a function repeatedly called at certain intervals. It is created by new_timer, which is passed the number of milliseconds which should elapse between calls to the given TimerFunction. A TimerFunction is a user defined function which is called when the timer activates, and is passed a pointer to the Timer.

A timer can be stopped using del_timer, which also deletes the timer's data structure from memory.

Timers are inexact and not likely to occur more often than at one tenth of a second intervals.