Windows Scripting: Sleeping from an HTA

Nov 09, 2004 19:40

This entry is a bit odd.

Few people will probably ever have the need to use the Sleep() subroutine from within an HTML document. And even if someone does, it still can't be used if the document is hosted on the Web. This entry is for a very specific audience.

In order to use the Sleep() subroutine from within a local HTML document or an HTML Application (HTA), I had to create a separate script file (sleep.vbs) and call it from my HTML document.

Here is sleep.vbs:

Dim Seconds

If WScript.Arguments.Count = 1 Then
    Seconds = Int(WScript.Arguments(0))
Else
    Seconds = 1
End If

WScript.Sleep(Seconds * 1000)
If the script is called incorrectly, it will default to sleeping for one second.

In order to use this from my HTML document, I defined a Sleep() subroutine as follows:

Sub Sleep(Seconds)
Dim Shell
    Set Shell = CreateObject("WScript.Shell") ' This is only needed if it isn't already defined.
    Shell.Run "sleep.vbs " & Seconds, 0, True
End Sub
If you look up the Run() method at the MSDN Library, you'll see that the last argument forces whatever is calling the subroutine (my HTML document) to stop until the first argument (in this case, sleep.vbs) has finished running. The second argument determines window style. The 0 here hides whatever window may otherwise open.

Finally, here is how to call the Sleep() subroutine:

Sleep(5) ' Sleep for 5 Seconds

wsh scripting, scripting, geeky, programming, windows

Previous post Next post
Up