Five methods can realize the delay in the batch, recommend the use of method one, the method is also the most used.
method One with ping command delay (This is the simplest and most common):
@echo off
echo "Use ping to delay"
Set Sleep=ping 127.0.0.1/n
Echo%time%
%sleep% 4 > nul
Echo%time%
Pause
method Two can also use A for loop for delay, for example:
@echo off
echo "Use for to delay"
Echo%time%
FOR/L%%i in (1,1,100) do echo%%i >nul
Echo%time%
Pause
method Three uses the choice command for delay :
@echo off
echo "Use choice to delay"
choice/t 3/c ync/cs/d y
if errorlevel 1 goto next
: Next
::/T: For pause seconds (0~9999) Here is the meaning of pause for 10 seconds
::/D: Specifies the default option for the time after nnnn seconds (that is, the/t setting).
::/C: To specify a list of options to create. The default list is "YN". If only for delay, here you can set the default option to Y, pause after the if go to the default label to execute the following command.
:: If you want to learn more, you can enter choice/in cmd?
method Four uses VBS for delay (can be accurate to milliseconds):
@echo off
echo "Use VBS to delay"
Echo%time%
Call:d Elay 3000
Echo%time%
Pause
Exit
:d Elay
Echo Wscript.Sleep%1>delay.vbs
CScript//b Delay.vbs
Del Delay.vbs
method Five purely with bat delay (most complex, can be accurate to milliseconds):
@echo off
set/p delay= Please enter the number of milliseconds you want to delay:
Set totaltime=0
Set nowtime=%time%
:: Read start time, time format: 13:01:05.95
Echo Program start time:%nowtime%
:d elay_continue
set/a minute1=1%nowtime:~3,2%-100
:: The number of minutes to read the start time
set/a second1=1%nowtime:~-5,2%%nowtime:~-2%0-100000
:: Converts the number of seconds from the start time to milliseconds
Set nowtime=%time%
set/a minute2=1%nowtime:~3,2%-100
:: Number of minutes to read the current time
set/a second2=1%nowtime:~-5,2%%nowtime:~-2%0-100000
:: Converts the number of seconds in the current time to milliseconds
set/a totaltime+= (%minute2%-%minute1%+60)%%60*60000+%second2%-%second1%
If%totaltime% lss%delay% goto delay_continue
Echo Program End time:%time%
echo Setting delay time:%delay% ms
echo Actual delay time:%totaltime% ms
Pause
Ways to implement latency in Windows batch processing