58

How to set delay in vbscript?

WScript.Sleep(100) does not work on Windows XP, Vista.

whytheq
  • 34,466
  • 65
  • 172
  • 267
Mark
  • 589
  • 1
  • 4
  • 3
  • 4
    *"`WScript.Sleep(100)` does not work on Windows XP, Vista"* Yes, it does (just tested in XP). It waits 100ms (a tenth of a second). If you want 100 *seconds*, then use 100 * 1000, as the value is in milliseconds. (And as this is VBScript, you don't need the parens. They don't actually break it, but it's not *right*...) – T.J. Crowder Jan 20 '15 at 18:35
  • Try `WSH.Sleep 1000` –  Aug 23 '19 at 11:21

12 Answers12

67

Work this end (XP).

Create a new file, call it test.vbs. Put this in it.

WScript.Sleep 1000
MsgBox "TEST"

Run it, notice the delay before the message box is shown.

Note, the number is in Milliseconds, so 1000 is 1 second.

badbod99
  • 7,429
  • 2
  • 32
  • 31
38

If you're trying to simulate a sleep delay in VBScript but WScript is not available (eg: your script is called from Microsoft's BGInfo tool), then try the following approach.

The example below will delay until 10 seconds from the moment the instruction is processed:

Dim dteWait
dteWait = DateAdd("s", 10, Now())
Do Until (Now() > dteWait)
Loop
  • 10
    it can work... but I'm worried about CPU usage of that solution (it's a long loop). Maybe in script without WScript available, you can call it with CreateObject, like indicated in this answer: http://stackoverflow.com/a/4037208/460362 – Stefano Aug 07 '13 at 14:10
  • Works in IE 11. – Flea Jan 23 '17 at 19:36
  • 2
    Terrible solution. But if something is stupid, but it works... It's not stupid. – Jakub Szułakiewicz Oct 27 '17 at 08:47
  • Worked for an HTA I had where it would not read the `WScript` shell object I created to do `objShell.Sleep()`, nor `WScript.Sleep()`. So I did Paulie's method instead. One caveat: in Chrome it gives you that pop-up if you want to wait for the script to respond if you put in anything more than 1 second. You can say 'Yes' and it'll work, but it would probably freak out many non-technical people. – vapcguy Jul 30 '18 at 16:29
  • ''' invoke a delay (5 seconds) from vbs inside HTA Set wshShell = CreateObject( "WScript.Shell" ) strCmd = wshShell.ExpandEnvironmentStrings( "%COMSPEC% /C (TIMEOUT.EXE /T 5 /NOBREAK)" ) wshShell.Run strCmd, 0, 1 Set wshShell = Nothing – Steve Wasiura Mar 14 '23 at 16:59
26

if it is VBScript, it should be

WScript.Sleep 100

If it is JavaScript

WScript.Sleep(100);

Time in milliseconds. WScript.Sleep 1000 results in a 1 second sleep.

Sachin Joseph
  • 18,928
  • 4
  • 42
  • 62
Jeeva Subburaj
  • 1,881
  • 2
  • 18
  • 26
  • @badbod99 that's like saying: as long as you aren't caught, stealing works as well as buying - "someSub (param)" will pass a *copy* of param to someSub. – Ekkehard.Horner Aug 27 '12 at 19:24
13

The following line will make your script to sleep for 5 mins.

WScript.Sleep 5*60*1000

Note that the value passed to sleep call is in milli seconds.

Raja Anbazhagan
  • 4,092
  • 1
  • 44
  • 64
12

A lot of the answers here assume that you're running your VBScript in the Windows Scripting Host (usually wscript.exe or cscript.exe). If you're getting errors like 'Variable is undefined: "WScript"' then you're probably not.

The WScript object is only available if you're running under the Windows Scripting Host, if you're running under another script host, such as Internet Explorer's (and you might be without realising it if you're in something like an HTA) it's not automatically available.

Microsoft's Hey, Scripting Guy! Blog has an article that goes into just this topic How Can I Temporarily Pause a Script in an HTA? in which they use a VBScript setTimeout to create a timer to simulate a Sleep without needing to use CPU hogging loops, etc.

The code used is this:

<script language = "VBScript">

    Dim dtmStartTime

    Sub Test
        dtmStartTime = Now 
        idTimer = window.setTimeout("PausedSection", 5000, "VBScript")
    End Sub

    Sub PausedSection
        Msgbox dtmStartTime & vbCrLf & Now
        window.clearTimeout(idTimer)
    End Sub

</script>

<body>
    <input id=runbutton  type="button" value="Run Button" onClick="Test">
</body>

See the linked blog post for the full explanation, but essentially when the button is clicked it creates a timer that fires 5,000 milliseconds from now, and when it fires runs the VBScript sub-routine called "PausedSection" which clears the timer, and runs whatever code you want it to.

GAThrawn
  • 280
  • 2
  • 13
7

Here's another alternative:

Sub subSleep(strSeconds) ' subSleep(2)
    Dim objShell
    Dim strCmd
    set objShell = CreateObject("wscript.Shell")
    'objShell.Run cmdline,1,False

    strCmd = "%COMSPEC% /c ping -n " & strSeconds & " 127.0.0.1>nul"     
    objShell.Run strCmd,0,1 
End Sub 
Flexo
  • 87,323
  • 22
  • 191
  • 272
Marc B. Hankin
  • 771
  • 3
  • 15
  • 31
4

Here is my solution. Worked with script, which was ran by third party program with no WScript declared and no import allowed.

Function MySleep(milliseconds)
  set WScriptShell = CreateObject("WScript.Shell")
  WScriptShell.Run "Sleep -m " & milliseconds, 0, true
end Function

Update
Looks like Microsoft removed Sleep.exe from win 8, so this doesn't work in win 8 unless you put Sleep.exe in folder defined in %path%.

Atomosk
  • 1,871
  • 2
  • 22
  • 26
4

Time of Sleep Function is in milliseconds (ms)

if you want 3 minutes, thats the way to do it:

WScript.Sleep(1000 * 60 * 3)
Nadir SOUALEM
  • 3,451
  • 2
  • 23
  • 30
4

better use timer:

Sub wait (sec)
    dim temp
    temp=timer
    do while timer-temp<sec
    loop
end Sub
  • this is a good solution, very straightforward, but when run from inside an HTA it generates an unresponsive script alert, asking if you want to stop running the script because it might lock up your computer. You can say no, but it still interrupts the normal flow of the program. – freginold Jan 20 '17 at 16:42
3

Here is an update to the solution provided by @user235218 that allows you to specify number of milliseconds you require.

Note: The -n option is the number of retries and the -w is the timeout in milliseconds for ping. I chose the 127.255.255.254 address because it is in the loopback range and ms windows doesn’t respond to it.

I also doubt this will provide millisecond accuracy but on another note i tried it in an application using the ms script control and whilst the built in sleep function locked up the interface this method didn't.

If somebody can provide an explanation for why this method didn't lock up the interface we could make this answer more complete. Both sleep functions where run in the user thread.

Const WshHide = 0
Const WAIT_ON_RETURN = True

Sub Sleep(ByVal ms)

   Dim shell 'As WScript.Shell

   If Not IsNumeric(ms) Then _
       Exit Sub

   Set shell = CreateObject("WScript.Shell")

   Call shell.Run("%COMSPEC% /c ping -n 1 -w " & ms & " 127.255.255.254 > nul", WshHide, WAIT_ON_RETURN)

End Sub
Robert
  • 3,328
  • 2
  • 24
  • 25
1

As stated in this answer:

Application.Wait (Now + TimeValue("0:00:01"))

will wait for 1 second

William Humphries
  • 541
  • 1
  • 10
  • 21
0
WScript.Sleep 100

The following code should work properly.

Stylerr.
  • 33
  • 6