Examples: CreateTimer method

This example maintains and reports the elapsed time since a document is opened, unless the user disables the timer. The example has several scripts.

  1. These declarations are in the (Globals) (Declarations) script for the form. The variable elapsedTime is used by various objects on the form. The NotesTimer object elapsedTimer must be in effect during the execution of a number of scripts on the form.
    Dim elapsedTime As Integer
    Dim elapsedTimer As NotesTimer
    %INCLUDE "lsconst.lss"
  2. When a document based on this form opens, the NotesTimer object is created with an interval of one second. An event handler for the Alarm event of the object is established.
    Sub Postopen(Source As Notesuidocument)
      Dim session As New NotesSession
      Set elapsedTimer = session.CreateTimer()
      elapsedTimer.Interval = 1
      elapsedTimer.Comment = _
      "Elapsed time since opening document"
      elapsedTime = 0
      On Event Alarm From elapsedTimer _
      Call elapsedTimerHandler
    End Sub
  3. This user sub is the event handler specified in the On Event statement above. It simply increments the global variable by 1 each time it is called.
    Sub elapsedTimerHandler(Source As NotesTimer)
      elapsedTime = elapsedTime + 1
    End Sub
  4. This script is for a button on the form. The script displays the time accumulated in the global variable.
    Sub Click(Source As Button)
      Dim etime As Integer
      Dim minutes As Integer
      etime = elapsedTime
      If etime < 60 Then
        Messagebox etime & " second(s)",, _
        "Elapsed time since opening document"
      Else
        minutes = 0
        Do While etime > 59
          minutes = minutes + 1
          etime = etime - 60
        Loop
        Messagebox minutes & " minute(s), " & _
        etime & " second(s)",, _
        "Elapsed time since opening document"
      End If
    End Sub
  5. This example queries the Enabled property of the NotesTimer object and toggles it if the user wants to.
Sub Click(Source As Button)
  If elapsedTimer.Enabled Then
    If Messagebox _
    ("Do you want to disable the timer?", _
    MB_YESNO + MB_ICONQUESTION, _
    "Elapsed timer is enabled") = IDYES Then
      elapsedTimer.Enabled = False
    End If
  Else
    If Messagebox _
    ("Do you want to enable the timer?", _
    MB_YESNO + MB_ICONQUESTION, _
    "Elapsed timer is disabled") = IDYES Then
      elapsedTimer.Enabled = True
    End If
  End If
End Sub