Is the Sleep operation no longer used in VBscript?
The "Sleep" command as stated in many places over the internet (including here on this forum) DOES NOT WORK. Is it now an obsolete command?
I am writing the VBScript code like this:
sub button1_onclick()
Wscript.Sleep 1000
div1.innerHTML = textbox1.value
end sub
It should wait 1 second and then execute that simple command. This is an utterly simple statement but it does not work. Plain and simple. It comes up with an error every time saying:
Object Required: 'Wscript'
Daniel's answer is absolutely correct about context being the key here. Although you don't have the WScript
method available, you do have the full browser DOM, including the window.setTimeout
method. With VBScript, the semantics of passing code to setTimeout
are a little bit different than JavaScript, but it's still possible:
Sub button1_onclick()
window.setTimeout GetRef("Delayed"), 1000
End Sub
Sub Delayed()
div1.innerHTML = textbox1.value
End Sub
Another option would be to (ab)use ping
(if you want to avoid an additional script):
Sub Sleep(seconds)
CreateObject("WScript.Shell").Run "%COMSPEC% /c ping 127.0.0.1 -n " _
& seconds+1, 0, True
End Sub
ping
sends echo requests in (roughly) 1 second intervals, so you can get an n
-second delay by sending n+1
echo requests.
When run from a browser, VBScript code does not have a Wscript object. This is only for stand-alone VBS. As such, Wscript.Sleep isn't obsolete, it just doesn't work in a browser without a work around.
This thread suggests a potential work around. The rest of this post comes from the entry by mayayana on the linked page:
If your security can allow WScript.Shell to run you can do it this way -
Script sub in webpage:
<SCRIPT LANGUAGE="VBScript">
Sub Sleep(NumberSeconds)
Dim SH, Ret
Set SH = CreateObject("WScript.Shell")
Ret = SH.Run("sleeper.vbs " & NumberSeconds, , True)
End Sub
</SCRIPT>
In the same folder as the webpage, put a file named sleeper.vbs and put this code into it:
Dim Arg
on error resume next
Arg = WScript.Arguments(0) * 1000
WScript.sleep Arg
You can then call something like:
Sleep 5
'-- pauses 5 seconds.