ping 的参数 n 与时间秒无直接关系。但可用以下方式实现ping来精确控制延时,精确度1秒,最短1秒,最长1天。- :: BAT纯批处理延时,精确到1秒 By Yu2n
- :: 示例:
- :: 1. 延时 10 秒:
- :: call :xSleep 10
- :: 2. 延时23小时:
- :: call :xSleep 82800
- :: 注意:不支持超过一天的延时(应小于86400秒)
-
- :: 代码如下
- @echo off
- setlocal enabledelayedexpansion
-
- call :xSleep 10
- pause
- goto :eof
-
- :: Call :xSleep_loop 5
- goto :eof
- :xSleep
- if not defined time_start (
- set/a n_sleep=%~1
- set time_start=%time%
- call :xSleep_Count !time_start!
- if not defined n_star set/a n_star=!dt1c!
- )
- call :xSleep_count %time%
- set/a n_now=%dt1c%
- set/a n_check=%n_star% + %n_sleep% - %n_now%
- if %n_check% geq 0 (
- if "%n_check%" neq "%n_check_last%" echo %n_check% %hns%
- set n_check_last=%n_check%
- ping -w 500 -n 2 0.0.0.0 >nul
- goto :xSleep
- )
- goto :eof
- :xSleep_Count
- set dt1=%~1
- for /f "delims=: tokens=1,2,3*" %%i in ("%dt1%") do (
- set dt1h=%%i
- if "!dt1h:~0,1!"=="0" set/a dt1h=!dt1h:~1,1!
- set dt1n=%%j
- if "!dt1n:~0,1!"=="0" set/a dt1n=!dt1n:~1,1!
- set dt1s=%%k
- set dt1s=!dt1s:~0,2!
- if "!dt1s:~0,1!"=="0" set/a dt1s=!dt1s:~1,1!
- )
- set "hns=%dt1h%:%dt1n%:%dt1s%"
- set/a dt1c=(%dt1h% * 60 * 60) + (%dt1n% * 60) + (%dt1s%)
- set/a dt1c=%dt1c% %% (60 * 60 * 24)
- goto :eof
复制代码
|