How to set delay in vbscript?
WScript.Sleep(100)
does not work on Windows XP, Vista.
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
As stated in this answer:
Application.Wait (Now + TimeValue("0:00:01"))
will wait for 1 second
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.
Time of Sleep Function is in milliseconds (ms)
if you want 3 minutes, thats the way to do it:
WScript.Sleep(1000 * 60 * 3)
better use timer:
Sub wait (sec)
dim temp
temp=timer
do while timer-temp<sec
loop
end Sub
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.