+ Reply to Thread
Results 1 to 2 of 2

Having multiple issues with countdown timer

Hybrid View

  1. #1
    Registered User
    Join Date
    09-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    53

    Having multiple issues with countdown timer

    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

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Having multiple issues with countdown timer

    Hi Telperion,

    It's difficult to solve your entire problem, without seeing all the code.

    What's problematic is changing the remaining time on the fly
    I'm assuming you change the time on the fly by calling 'btnStartSnakeTimer_Click'.

    Try the following to change the time on the fly:
    a. Add a global time variable
     Public myTime As Date
    b. Change the way you schedule the timer to use the global time variable
     myTime = Now + TimeSerial(0, 0, 1)
     Application.OnTime myTime, "RunSnakeTimer"
    c. When you change the code on the fly (in btnStartSnakeTimer_Click()') , first stop the timer (if it is running) using the global scheduled time. A runtime error will be generated if the timer is NOT running, hence the need for 'On Error Resume Next'. Then you can start the timer again.

    Code snippet out of context:
     
     'Stop the timer (if it is running)
     On Error Resume Next
     Application.OnTime EarliestTime:=myTime,     Procedure:="RunSnakeTimer", Schedule:=False
     On Error GoTo 0
    
     'Restart the timer
     nTime = nCountSnake
     TimerCode.RunSnakeTimer
    Lewis

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Displaying a unique countdown timer with Start/Stop/Reset buttons on multiple lines
    By drememagik in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-07-2017, 07:51 PM
  2. [SOLVED] Countdown timer
    By flyinghigher2011 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-10-2013, 03:08 PM
  3. [SOLVED] Multiple Countdown/Timer
    By sdheckler in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 06-04-2013, 06:02 PM
  4. Countdown Timer
    By gkokaisel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-13-2011, 08:02 PM
  5. Countdown Timer
    By Zaphius in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2007, 06:14 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1