Jump to content

Recommended Posts

Hello,

i have been making a countdown timer and its all working just how i like it, its just skipping the 0's en going from 1 - 59 ( it does wait the time needed for the counter to go from 1 - 0 - 59) It just goes from 1 to 59, showing the 1 for the time needed to go from 1 to 0 and then to 59.

 

Here's my code:

For $i = 60 To 1 Step -1
        Next

        If $i = 0 Then
            $Sc = $Sc - 1
        EndIf

        If $Sc = 0 Then
            $Sc = 0  ;~   <--- Shouldnt this work? Setting it to 0 then wait 1 sec to set it to 59?
            Sleep(1000)
            $Mn = $Mn - 1
            $Sc = 59
        EndIf


        If $Mn = 0 And $Sc = 0 Then
            $CDHrs = 0
            Sleep(1000)
            $Mn = 59
        EndIf

        GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
        Sleep(1000)

I dont need the solution in code, just point me into the general direction where i am going wrong, i'd like to learn from my mistake. Thanks in advance.

 

~ Maffiagang

Edited by Maffiagang
Link to comment
Share on other sites

Here, it would take too long to tell you what it is wrong.  But remember one thing, it is a bad idea to put Sleep inside the GUI loop.  Other than that, study the script, if you have more question, just let me know :

#include <GUIConstants.au3>

GUICreate ("Test")
Local $Countdown = GUICtrlCreateLabel ("", 50, 50, 300, 30)
GUISetState()

Local $CDHrs = 1, $Mn = 10, $Sc = 20
GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
Local $hTimer = TimerInit()

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
  EndSwitch
  If TimerDiff($hTimer) < 1000 Then ContinueLoop
  $hTimer = TimerInit()
  $Sc -= 1
  If $Sc < 0 Then
    $Mn -= 1
    If $Mn < 0 Then
      $CDHrs -= 1
      If $CDHrs < 0 Then ExitLoop
      $Mn = 59
    EndIf
    $Sc = 59
  EndIf
  GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
WEnd

 

Edited by Nine
corrected a small bug
Link to comment
Share on other sites

A different approach (banged my head more than once on various hard places while trying to change Excel formula to AutoIT :idiot: )

Someone has to verify but seems more accurate than Nine's code. Avg. CPU loads looks similar for both (2.5-4%).

Console Output:

120011.8351 - 120000

#include <GUIConstants.au3>

GUICreate ("Test")
Local $Countdown = GUICtrlCreateLabel ("", 50, 50, 300, 30)
GUISetState()

Local $CDHrs = 0, $Mn = 2, $Sc = 0
GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)

Local $iCountDownTime = ($CDHrs*60*60+$Mn*60+$Sc)*1000
Local $hTimer = TimerInit()
Local $bTimerRunning = True
Local $iCounter = 0

While True
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
        ExitLoop
    EndSwitch

    If $bTimerRunning Then

        Local $iSc = floor(mod(60+$Sc-mod(int(TimerDiff($hTimer)/1000),60),60))
        Local $iMn = Int(mod((60+$Mn-Floor(TimerDiff($hTimer)/1000-$Sc)/60),60))
        Local $iCDHrs = Int(mod(($CDHrs-Floor(TimerDiff($hTimer)/1000-$Sc-60*$Mn)/(60*60)),60))

        if $iCounter <> Int(TimerDiff($hTimer)/1000) Then
            GUICtrlSetData($Countdown, 'Hrs:'  & $iCDHrs & ' ' & 'Min:' & $iMn & ' ' & 'Sec:' & $iSc)
            $iCounter = Int(TimerDiff($hTimer)/1000)
        EndIf

        If $iSc=0 and $iMn=0 and $iCDHrs= 0 Then
            ConsoleWrite(TimerDiff($hTimer) & " - " & $iCountDownTime & @CRLF)
            $bTimerRunning=False
        EndIf

    EndIf
    
WEnd

 

Modified Nine's Code to include a second timer (I hope it is accurate?).

Console Output:

122316.3811

#include <GUIConstants.au3>

GUICreate ("Test")
Local $Countdown = GUICtrlCreateLabel ("", 50, 50, 300, 30)
GUISetState()

Local $CDHrs = 0, $Mn = 2, $Sc = 0
GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
Local $hTimer = TimerInit()
Local $hTimer2 = TimerInit()

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
  EndSwitch
  If TimerDiff($hTimer) < 1000 Then ContinueLoop
  $hTimer = TimerInit()
  $Sc -= 1
  If $Sc < 0 Then
    $Mn -= 1
    If $Mn < 0 Then
      $CDHrs -= 1
      If $CDHrs < 0 Then
          ConsoleWrite(TimerDiff($hTimer2) & @CRLF)
          ExitLoop
      EndIf
      $Mn = 59
    EndIf
    $Sc = 59
  EndIf
  GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
WEnd

 

Link to comment
Share on other sites

On 12/22/2020 at 8:25 PM, Nine said:

Here, it would take too long to tell you what it is wrong.  But remember one thing, it is a bad idea to put Sleep inside the GUI loop.  Other than that, study the script, if you have more question, just let me know :

#include <GUIConstants.au3>

GUICreate ("Test")
Local $Countdown = GUICtrlCreateLabel ("", 50, 50, 300, 30)
GUISetState()

Local $CDHrs = 1, $Mn = 10, $Sc = 20
GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
Local $hTimer = TimerInit()

While True
  Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
      ExitLoop
  EndSwitch
  If TimerDiff($hTimer) < 1000 Then ContinueLoop
  $hTimer = TimerInit()
  $Sc -= 1
  If $Sc < 0 Then
    $Mn -= 1
    If $Mn < 0 Then
      $CDHrs -= 1
      If $CDHrs < 0 Then ExitLoop
      $Mn = 59
    EndIf
    $Sc = 59
  EndIf
  GUICtrlSetData($Countdown, 'Hrs:'  & $CDHrs & ' ' & 'Min:' & $Mn & ' ' & 'Sec:' & $Sc)
WEnd

 

I knew it should be possible with only ifs instead of using for, thank you for this, ive added it to my code, Thanks!

Btw, i'm using OnEventMode and that piece of code was not in my main while but in a different func. My main while only consists of a waiting loop for the start button to be pressed. :)

It was my mistake for not posting the full piece of my code.

I was going this way before, but i was using my own timer and another timer which substracted from themselves, by which i was getting a time. 

(Somehow with round and substracting the hours / mins / secs from that value, i could only get the last 30 sec of each minute to display (derp) 

Probably had to do something with me having to add a - 0.5 to each equation to get it to work entirely..._ i couldnt get it to work by getting the full 1 (or 2) digit without any decimal (it would always round up or down, which it shouldnt in my case)

Your way is SO much simpler than what i was trying before i tried using for. Thanks again. :) 

Edited by Maffiagang
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...