First off, how I'm using this and what I have working. I'm using it in a user form and it's being displayed in a label. I need to be able to start the timer, stop the timer, reset the timer, and change the amount of time remaining on the fly. Start, stop, reset, are easy. What's problematic is changing the remaining time on the fly. Sometimes it works, sometimes it doesn't, and I can't quite always figure out why. I believe it's related to the "nTime = nTime -1"; when the timer gets triggered a second time, it counts down at double speed. I'm not sure how to prevent that. Here's the code.
If there is a better way to run the countdown, I'm totally on board with switching out the fundamental timer code, because the way it's currently set up requires me to use If/Then statements in the subroutines that call the timer because I have to first test to see if it's running. If it's already running, I have to set nTime = nCountSpacer in the calling subroutine, then direct it to a separate timer code where nTime = nTime (Because the first triggered timer is already counting down at - 1). If I send it to the nTime = nTime - 1 again, it counts down too fast. It's getting tedious.
Option Explicit
Public Const nCountSnake As Long = 60 'secs
Public Const nCountSpacer As Long = 15 'secs
Public nTime As Double
Public Sub RunSnakeTimer()
If nTime > 1 Then
nTime = nTime - 1
DraftWindow.lblSnakeTimer.Caption = Format(TimeSerial(0, 0, nTime), "hh:mm:ss")
Application.OnTime Now + TimeSerial(0, 0, 1), "RunSnakeTimer"
Else
DraftWindow.lblSnakeTimer.BackColor = &HFF&
DraftWindow.lblSnakeTimer.Caption = "EXPIRED"
Beep
Beep
Beep
End If
End Sub
Private Sub btnStartSnakeTimer_Click()
lblSnakeTimer.BackColor = &H8000000F
If nTime > 1 Then
DraftWindow.Repaint
Else
nTime = nCountSnake
TimerCode.RunSnakeTimer
End If
End Sub
Bookmarks