5

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'

Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40
Cheesus Toast
  • 1,043
  • 2
  • 15
  • 21
  • What are you trying to accomplish? A 1 second delay after the button is clicked, before it responds? Is this a requirement? – Daniel Oct 27 '12 at 01:23
  • @Daniel Cook: Yes, I was just trying to create a delay before the next operation in the procedure. This code was only a really simple bit of stuff to illustrate what I was trying to do. – Cheesus Toast Oct 27 '12 at 17:37

3 Answers3

5

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
Community
  • 1
  • 1
Cheran Shunmugavel
  • 8,319
  • 1
  • 33
  • 40
  • Oh wow, thank you Cheran... that was the problem I was having. It was that I was trying to use the JavaScript syntax of setTimeout. I will try this when I get time. I have been using w3schools.com mostly and they didn't mention the timeout function. I could not find it anywhere else either. – Cheesus Toast Oct 27 '12 at 17:31
4

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Looks like an interesting way to manipulate an external source. I think the setTimeout syntax was all I really needed though in the end. :) – Cheesus Toast Oct 27 '12 at 17:39
2

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.

Daniel
  • 12,982
  • 3
  • 36
  • 60
  • Thank you for your response. I have tried that and to be honest it is too long winded and it would not be acceptable for a college project. I can't start playing with the security features to get the program to work. It is still not working anyway. Is there any other way to implement a timer into VBscript? I tried setTimeOut but that does not seem to work in this language, only javascript. I hate VBscript btw... but this is the language of choice for the college unfortunately :( – Cheesus Toast Oct 27 '12 at 01:15