Page 1 of 1

Live coundown timer

Posted: 29 Oct 2021 11:34
by drgt
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.

Re: Live coundown timer

Posted: 29 Oct 2021 12:14
by Squashman

Re: Live coundown timer

Posted: 29 Oct 2021 12:37
by T3RRY
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

Re: Live coundown timer

Posted: 29 Oct 2021 12:51
by drgt
That run for about 5 seconds. Also flickers something else too fast to read...

Timeout not recognized command...

Re: Live coundown timer

Posted: 29 Oct 2021 13:00
by atfon
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.

Re: Live coundown timer

Posted: 29 Oct 2021 13:11
by drgt
xp sp2!

Re: Live coundown timer

Posted: 29 Oct 2021 23:01
by T3RRY
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);"

Live coundown timer

Posted: 30 Oct 2021 00:19
by drgt
Thank you very much!

Re: Live coundown timer

Posted: 30 Oct 2021 00:28
by Gerhard
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

Re: Live coundown timer

Posted: 01 Nov 2021 06:06
by atfon
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.

Re: Live coundown timer

Posted: 01 Nov 2021 22:59
by Gerhard
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.