Script programmers need several times something like a wait function to delay the execution of statements within a script (for instance wait till a program was ready). Many people tries to use a simple loop to delay the execution. This causes two problems: The delay time depends on your CPU speed, and the CPU is used during the wait period.
A much smarter approach is to use the Popup method provided by the Shell object. This method supports one parameter defining the timeout period. The following sequence shows how to use this technique:
Set wsh = WScript.CreateObject("WScript.Shell") wsh.Popup "Wait 5 seconds using Popup", 5, "Wait...",64
The 1st parameter contains the text shown in the dialog. The 2nd parameter defines the wait period of 5 seconds, the last parameter defines the code for the buttons shown in the dialog box. If the user doesn't click the OK button to close the dialog box, this box is getting closed automatically after the timeout period. This approach allows you to implement an excact delay, and during the script displays the dialog box, no CPU time is used. But there ist always a risk that the user clicks onto the OK button (in this case the delay time isn't reached - there are tricks to check whether the required delay time is processed, and if not to re-invoke the Popup method to delay the script again - but this is complicated and not necessary).
If you need a silent and more convenient delay method, you can use either my WSHWait method implemented in the WSHExtend control, or you can use the WScript Sleep method provides in WSH 2. If you have downloaded the ActiveX control and install it on your machine, you can implement a delay with two lines of code:
Set objAdr = WScript.CreateObject("WSHExtend.WinExt") objAdr.WSHWait 5000
The second line causes Windows to supend the script for 5 seconds. No CPU time will be used during this time. In JScript use the following statement:
var objAdr = WScript.CreateObject("WSHExtend.WinExt"); objAdr.WSHWait (5000);
If you use WSH 2 on your maschine I recommend to use the WSH 2 Sleep method (because you need not to install an additional ActiveX control). Here is the VBScript code to suspend a script for 5 seconds:
In JScript you need to use following statement:
Well, that's all. Additional information about this sample and the source code of the WSHExtend ActiveX control may be found in my German WSH book Inside Windows Script Host, published by Microsoft Press. A broad discussion how to use Sleep may be found also in Microsoft Windows Script Host 2.0 Developer's Guide (Microsoft Press).
(c) G. Born