Windows批处理:延时
以打开一个文件为例,打开文件使用 start 命令即可。
方法一:ping
缺点:时间精度为1秒,不够精确
@echo off
@ping 127.0.0.1 -n 6 >nul
方法二:vbs start /wait
优点:时间精度为0.001秒,精度高
缺点:生成临时文件
@echo off
echo wscript.sleep 5000>sleep.vbs
start /wait sleep.vbs
del /f /s /q sleep.vbs
方法三:vbs cscript.
@echo off
echo wscript.sleep 5000>sleep.vbs
@cscript sleep.vbs >nul
del /f /s /q sleep.vbs
方法四:choice
优点:时间精确,CPU占用低,是最佳选择
@echo off
choice /t 5 /d y /n >nul
方法五:timeout
timeout [/t] timeout [/nobreak]
在windows vista及以上系统中,系统提供了 timeout 命令。
优点:方便,一行命令搞定。
缺点:不能在旧系统中(例如xp)使用,且延时精度较低(1秒)。