Is the Sleep operation no longer used in VBscript?

后端 未结 3 1599
清歌不尽
清歌不尽 2020-12-02 01:14

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 VBScri

相关标签:
3条回答
  • 2020-12-02 01:18

    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.

    0 讨论(0)
  • 2020-12-02 01:19

    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
    
    0 讨论(0)
  • 2020-12-02 01:22

    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.

    0 讨论(0)
提交回复
热议问题