如何在 Bat 檔案中睡眠或等待 X 秒
Jinku Hu
2023年1月30日

timeout
(> Windows 7 / 2008)
timetout
將執行延遲指定的秒數。
語法
timeout <seconds> /nobreak
<seconds>
是介於 -1 和 100000 之間的任何值。-1
使計算機無限期地等待直到鍵盤鍵擊。
/nobreak
忽略使用者按鍵,等待不會受到任何按鍵干擾。
timeout
舉例
timeout /t 5
它將在下一次執行之前暫停 5 秒。
ping
引入延遲的命令
儘管在使用 timeout
命令時 PC 會暫停命令執行幾秒鐘,但它仍會消耗大量 CPU 負載。帶有回送地址 127.0.0.1
的 ping
命令在每個連續的 ping
之間還會產生 1 秒的延遲,並且消耗的 CPU 負載更少。
ping
延遲示例
ping -n 6 127.0.0.1>nul
6
表示延遲設定為 6-1=5
秒。
>nul
將標準輸出流傳輸到管道 null
,因此控制檯中沒有顯示任何資訊。
作者: Jinku Hu
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn