8

I want to make a batch file wait for 0.5 or 0.25 seconds.
I have already tried timeout /t 0.5 but it is not working.

It is something like:
@echo off color a cls :top echo %random% timeout /t 0.5 echo %random% goto top

Edit : Thanks for the answers y'all

magnus167
  • 109
  • 1
  • 1
  • 10

3 Answers3

11

Try this: ping 127.0.0.1 -n 1 -w 500> nul 500 is the time in ms.

EDIT:

As rojo posted in the comments this won't work as expected for 250ms:

ping used this way pauses a minimum of 500ms. Using any value lower than 500 still results in a half second pause

MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • 1
    To clarify here: The address to be pinged must not reply, otherwise the `-w` parameter does not apply. – Joey Jun 05 '15 at 12:04
  • 3
    `ping` used this way pauses a minimum of 500ms. Using any value lower than 500 still results in a half second pause. – rojo Jun 05 '15 at 12:21
  • @rojo: Good to know, thx! – MichaelS Jun 05 '15 at 12:24
  • You can't do it with `timeout`. That's why I told you to use ping instead ;-) – MichaelS Jun 19 '15 at 08:37
  • Amazing hack man! – magnus167 Aug 18 '17 at 09:29
  • This doesn't correctly answer the original question, which asks to make a batch file wait for 0.5 or 0.25 seconds. As stated, any value lower than 500ms still results in a 500ms pause, and yet a misleading 250ms is used. The answer is noteworthy at best. – cjsimon Dec 16 '17 at 08:12
  • Why was this downvoted is beyond me. Thanks a lot!!! It's an awesome hack – Gaspa79 Feb 28 '18 at 01:17
  • 1
    @Gaspa79 The downvote is correct. 250 is a valid parameter but it won't work as expected. The lower bound for the ping delay is 500ms so even if you pass 250 as a parameter it will still be interpreted as 500. – MichaelS Feb 28 '18 at 10:10
  • 2
    Please use 192.0.2.0. This is a test ip range. https://en.wikipedia.org/wiki/Reserved_IP_addresses, this method will fail if 1.1.1.1 exists (which is now a dns ip that cloudflare owns) – WHol May 10 '18 at 09:05
  • @WHol thx, I've updated the answer. – MichaelS May 11 '18 at 09:04
  • 1
    500 isn't just the lower bound, it is the timing resolution. 1499 will round down to 1000 and 1999 will round down to 1500. It's always in multiples of 500 milliseconds. – pbarney Jan 15 '20 at 15:25
  • 1
    Pinging `127.0.0.1` will not work as it will answer immediately. – EvgenKo423 Jun 15 '22 at 15:22
10

TIMEOUT only works in increments of whole seconds.

There are other third-party "sleep" executables you can download.

Alternatively, you could use the Sleep method in VBScript. It accepts values as milliseconds. Here is a simple example (save it to SLEEP.VBS or whatever name you want):

ms = WScript.Arguments(0)
WScript.Sleep ms

Then, in your batch file call it like this for a 0.5 second delay:

CSCRIPT SLEEP.VBS 500
aphoria
  • 19,796
  • 7
  • 64
  • 73
5

See this question for some worthwhile information. To summarize, using ping to pause is limited to 500ms or greater. You won't be able to pause for 250ms. The best solution is to use Windows Scripting Host -- VB Script or JScript.

You can do this with a batch / JScript hybrid to avoid having to call an external VBS script. Save this example with a .bat extension and try it.

@if (@CodeSection == @Batch) @then

@echo off
setlocal

for /L %%I in (10,-1,1) do (
    set /P "=%%I... "<NUL
    call :pause 250
)

echo Done.
goto :EOF

:pause <ms>
cscript /nologo /e:JScript "%~f0" "%~1"
goto :EOF

@end // end batch / begin JScript hybrid code
WSH.Sleep(WSH.Arguments(0));
Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • 1
    That's handy. You made my fast and ugly reset/wakeup batch file with GPIO utility pin toggles run 4x faster now. It would probably work fine @ 100ms too. – Barleyman Oct 26 '21 at 17:38
  • @Barleyman Thanks! I'm useful sometimes, even if by accident. :) – rojo Oct 26 '21 at 17:39