Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a batch file wait for fractions of a second? [duplicate]

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

like image 470
magnus167 Avatar asked Jun 05 '15 11:06

magnus167


People also ask

How to make a batch file wait for a number of seconds?

To make a batch file wait for a number of seconds there are several options available: 1 PAUSE 2 SLEEP 3 TIMEOUT 4 PING 5 NETSH (Windows XP/Server 2003 only) 6 CHOICE 7 CountDown 8 SystemTrayMessage 9 Other scripting languages 10 Unix ports

How do I delay a batch file in Linux?

There are three main commands you can use to delay a batch file: PAUSE — Causes the batch file to pause until a standard key (e.g., the spacebar) is pressed. TIMEOUT — Prompts the batch file to wait for a specified number of seconds (or a key press) before proceeding.

How do I pause a batch file while writing?

If you are writing a batch file and you don’t want to continue until somebody presses a key, you can do it really easy with the timeout command. For instance, using the following on the command prompt will pause the terminal for 10 seconds unless you press a key: timeout /t 10.

How to get another window from a batch file?

Use call ar.bat to do it completely with batch file commands. Use start /wait ar.bat to get another window and wait for it to complete. Thanks for contributing an answer to Stack Overflow!


2 Answers

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

like image 77
MichaelS Avatar answered Sep 30 '22 19:09

MichaelS


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
like image 45
aphoria Avatar answered Sep 30 '22 18:09

aphoria