Many scripters have gotten all too used to using the WScript object’s Sleep method for pausing or delaying script execution. Since this object isn’t available in ASP, Windows Script Components, or HTML Applications, programmers are required to create viable alternatives.
A Google search will return a plethora of possibilities each with their own pros and cons. Several third-party controls have also been created for this purpose. However, I prefer to stick to purely native means. I wanted a pure VBScript sleep function. Here’s what I came up with.
Sub Sleep(intSeconds) dteStart = Time() dteEnd = DateAdd("s", intSeconds, dteStart) While dteEnd > Time() DoNothing Wend End Sub Sub DoNothing 'While/Wend has quirks when it is empty End Sub
This is a simple Sleep method as a subroutine. It accepts a single integer value indicating the amount of time to sleep in seconds. It then snapshots the current time and adds the specified number of seconds to find an expiration time in the future.
A While/Wend loop is used as a timer until that expiration time is reached indicating that the specified number of seconds have elapsed.
Since loops in VBScript don’t always function well when they are empty or contain only comments, I’ve allowed my loop to make a call to an empty subroutine instead.
The syntax for this method is identical to that of WScript’s own Sleep method. This should work in any ASP, Windows Script Component (WSC), or HTML Application.