How to set delay in vbscript?
WScript.Sleep(100)
does not work on Windows XP, Vista.
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:
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.