Live coundown timer

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Live coundown timer

#1 Post by drgt » 29 Oct 2021 11:34

Is it possible to write code that would echo a countdown timer in "minutes : seconds" of a time value that I specify? say 10 min 30 sec.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Live coundown timer

#2 Post by Squashman » 29 Oct 2021 12:14


T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: Live coundown timer

#3 Post by T3RRY » 29 Oct 2021 12:37

yes, this is easily possible, however, more information regarding the use case would be helpful. IE: does it matter if the timer is script blocking, or do you want to just show a timer while the script is executing something else?

An example of a no frills timer that blocks script execution:

Code: Select all

@ECHO off

Call:Timer 10 30
Echo(done
Goto:Eof


:Timer minutes seconds
cls
setlocal
2> nul Set /A "m=%~1" || Exit /b 1
2> nul Set /A "s=%~2"
Set not0=2^>nul Set /A 1/
If not defined s Set s=0
:tloop
 If %s% LSS 10 (Set os=0)Else set "os="
 If %m% LSS 10 (Set om=0)Else set "om="
 Echo(%om%%m%:%os%%s%
 %not0% s && ( Set/A s-=1 ) || ( %not0% m && ( Set/A m-=1,s=59 ) || ( Endlocal & Exit/b ) )
Timeout /t 1 /NoBreak > nul 2>&1 || (
 start "" /w /b /min mshta "javascript:setTimeout(function(){close();},1000);"
)
cls
Goto:tloop
Last edited by T3RRY on 30 Oct 2021 03:49, edited 5 times in total.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Live coundown timer

#4 Post by drgt » 29 Oct 2021 12:51

That run for about 5 seconds. Also flickers something else too fast to read...

Timeout not recognized command...

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Live coundown timer

#5 Post by atfon » 29 Oct 2021 13:00

drgt wrote:
29 Oct 2021 12:51

Timeout not recognized command...
Which operating system and version are you running? The timeout command has been available as part of the standard Windows OS since Windows 7, IIRC.

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Live coundown timer

#6 Post by drgt » 29 Oct 2021 13:11

xp sp2!

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: Live coundown timer

#7 Post by T3RRY » 29 Oct 2021 23:01

drgt wrote:
29 Oct 2021 13:11
xp sp2!
Information like this is useful to provide when asking a question.

An alternative to timeout for you to try:

Code: Select all

start "" /w /b /min mshta "javascript:setTimeout(function(){close();},1000);"

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Live coundown timer

#8 Post by drgt » 30 Oct 2021 00:19

Thank you very much!

Gerhard
Posts: 11
Joined: 25 Oct 2021 22:01

Re: Live coundown timer

#9 Post by Gerhard » 30 Oct 2021 00:28

Here is another one created by Aacini on Stackoverflow some years back.

Code: Select all

@echo off
setlocal

rem Get end time
REM for /F "tokens=1,2 delims=:" %%a in ("ResponseTime.txt") do set /A endH=10%%a%%100, endM=1%%b%%100

REM Just for testing:
set endH=14
set endM=58

title Timer
mode con cols=16 lines=2

:synchronize
for /F "tokens=1,2 delims=:" %%a in ("%time%") do set /A "minutes=(endH*60+endM)-(%%a*60+1%%b-100)-1, seconds=159"

:wait
timeout /T 1 /NOBREAK > NUL
echo Timer:  %minutes%:%seconds:~-2%
set /A seconds-=1
if %seconds% geq 100 goto wait
set /A minutes-=1, seconds=159, minMOD5=minutes %% 5
if %minutes% lss 0 goto :buzz
if %minMOD5% equ 0 goto synchronize
goto wait

:buzz
pause

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Live coundown timer

#10 Post by atfon » 01 Nov 2021 06:06

Gerhard wrote:
30 Oct 2021 00:28
Here is another one created by Aacini on Stackoverflow some years back.

Code: Select all

:wait
timeout /T 1 /NOBREAK > NUL
That script also uses timeout, which was not part of the standard OS for Windows XP.

Gerhard
Posts: 11
Joined: 25 Oct 2021 22:01

Re: Live coundown timer

#11 Post by Gerhard » 01 Nov 2021 22:59

atfon wrote:
01 Nov 2021 06:06
Gerhard wrote:
30 Oct 2021 00:28
Here is another one created by Aacini on Stackoverflow some years back.

Code: Select all

:wait
timeout /T 1 /NOBREAK > NUL
That script also uses timeout, which was not part of the standard OS for Windows XP.
Yeah, I honestly did not take note of the Windows XP comment. Thanks.

Post Reply