Android Question Countdown timer is "skipping" to show any seconds

jeronimovilar

Active Member
Licensed User
Longtime User
First: Sorry my english

My function is working to countdown my time (TEMPO), but sometimes, it´s not show the second.
Ex: My function show the time in btnTIMER.Text (button): 10:09. (1s)..10:08.(1s)..10:07....(3s)......10:06.10:05.10:04
It doesn't show the time every second as it should
What can i do??

TIMER:
Private Sub countDown(seconds As Int)
    Dim StartTime As Long = DateTime.Now + (TEMPO * 60000) + (seconds * 1000)
    Dim  hours, minutes, seconds As Int
'    Dim countDownEnabled = True
    Do While countDownEnabled
        Dim count As Int = (DateTime.now - StartTime) / 1000
        If count >= seconds Then
            countDownEnabled = False
            btnTIMER.text = "00:00"
            btnTIMER.Enabled = True
            Exit
        End If
        'Log(seconds - count)  'or update display
        
        t = Abs(count)               
        hours = t / DateTime.TicksPerHour
        minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
        seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
        
        minutes = t/60
        seconds = t - minutes * 60
        If t < 0 Then
            btnTIMER.text = "00:00"
            btnTIMER.Enabled = True
        Else   
            btnTIMER.text = NumberFormat(minutes, 2, 0) & ":" & NumberFormat(seconds, 2, 0)
            If btnTIMER.Text="00:00" Then ' ----------- 0 min
                Timer2.Enabled = False
                Vibra_Toca (True, True, 0)
                Sleep(5000)
                btnTIMER_LongClick
            End If
        End If
        Sleep(400)
    Loop
End Sub
 

JohnC

Expert
Licensed User
Longtime User
To use your code (instead of using a timer), ChatGPT says...

It seems the countdown function is not properly updating the btnTIMER.Text every second due to the timing of the Sleep function and how the loop is structured. To ensure the countdown updates every second, we can make a few adjustments:
  1. Calculate the time remaining based on the initial start time.
  2. Use a more precise timer mechanism instead of Sleep to update the UI.
  3. Ensure the Sleep duration is set to 1000 milliseconds (1 second) to match the desired countdown interval.
Here's a revised version of your countdown function:

B4X:
Private Sub countDown(seconds As Int)
    Dim StartTime As Long = DateTime.Now
    Dim totalSeconds As Int = TEMPO * 60 + seconds
    Dim countDownEnabled As Boolean = True

    Do While countDownEnabled
        Dim elapsedSeconds As Int = (DateTime.Now - StartTime) / 1000
        Dim remainingSeconds As Int = totalSeconds - elapsedSeconds

        If remainingSeconds <= 0 Then
            countDownEnabled = False
            btnTIMER.Text = "00:00"
            btnTIMER.Enabled = True
            Exit
        End If

        Dim minutes As Int = remainingSeconds / 60
        Dim seconds As Int = remainingSeconds Mod 60
       
        btnTIMER.Text = NumberFormat(minutes, 2, 0) & ":" & NumberFormat(seconds, 2, 0)
       
        If btnTIMER.Text = "00:00" Then ' ----------- 0 min
            Timer2.Enabled = False
            Vibra_Toca(True, True, 0)
            Sleep(5000)
            btnTIMER_LongClick
        End If

        Sleep(1000) ' Sleep for 1 second
    Loop
End Sub

Changes made:
  1. Calculated the totalSeconds as the total countdown duration in seconds.
  2. Calculated elapsedSeconds as the difference between the current time and the start time.
  3. Updated the remainingSeconds based on totalSeconds minus elapsedSeconds.
  4. Used a Sleep(1000) to ensure the UI updates every second.
This should ensure that the countdown updates the button text every second consistently.
 
Upvote 0

jeronimovilar

Active Member
Licensed User
Longtime User
To use your code (instead of using a timer), ChatGPT says...

It seems the countdown function is not properly updating the btnTIMER.Text every second due to the timing of the Sleep function and how the loop is structured. To ensure the countdown updates every second, we can make a few adjustments:
  1. Calculate the time remaining based on the initial start time.
  2. Use a more precise timer mechanism instead of Sleep to update the UI.
  3. Ensure the Sleep duration is set to 1000 milliseconds (1 second) to match the desired countdown interval.
Here's a revised version of your countdown function:

B4X:
Private Sub countDown(seconds As Int)
    Dim StartTime As Long = DateTime.Now
    Dim totalSeconds As Int = TEMPO * 60 + seconds
    Dim countDownEnabled As Boolean = True

    Do While countDownEnabled
        Dim elapsedSeconds As Int = (DateTime.Now - StartTime) / 1000
        Dim remainingSeconds As Int = totalSeconds - elapsedSeconds

        If remainingSeconds <= 0 Then
            countDownEnabled = False
            btnTIMER.Text = "00:00"
            btnTIMER.Enabled = True
            Exit
        End If

        Dim minutes As Int = remainingSeconds / 60
        Dim seconds As Int = remainingSeconds Mod 60
      
        btnTIMER.Text = NumberFormat(minutes, 2, 0) & ":" & NumberFormat(seconds, 2, 0)
      
        If btnTIMER.Text = "00:00" Then ' ----------- 0 min
            Timer2.Enabled = False
            Vibra_Toca(True, True, 0)
            Sleep(5000)
            btnTIMER_LongClick
        End If

        Sleep(1000) ' Sleep for 1 second
    Loop
End Sub

Changes made:
  1. Calculated the totalSeconds as the total countdown duration in seconds.
  2. Calculated elapsedSeconds as the difference between the current time and the start time.
  3. Updated the remainingSeconds based on totalSeconds minus elapsedSeconds.
  4. Used a Sleep(1000) to ensure the UI updates every second.
This should ensure that the countdown updates the button text every second consistently.

Thanks. I´ll try
 
Upvote 0
Top