How to set delay in vbscript

前端 未结 11 1402
鱼传尺愫
鱼传尺愫 2020-12-03 09:35

How to set delay in vbscript?

WScript.Sleep(100) does not work on Windows XP, Vista.

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 09:56

    A lot of the answers here assume that you're running your VBScript in the Windows Scripting Host (usually wscript.exe or cscript.exe). If you're getting errors like 'Variable is undefined: "WScript"' then you're probably not.

    The WScript object is only available if you're running under the Windows Scripting Host, if you're running under another script host, such as Internet Explorer's (and you might be without realising it if you're in something like an HTA) it's not automatically available.

    Microsoft's Hey, Scripting Guy! Blog has an article that goes into just this topic How Can I Temporarily Pause a Script in an HTA? in which they use a VBScript setTimeout to create a timer to simulate a Sleep without needing to use CPU hogging loops, etc.

    The code used is this:

    
    
    
        
    
    

    See the linked blog post for the full explanation, but essentially when the button is clicked it creates a timer that fires 5,000 milliseconds from now, and when it fires runs the VBScript sub-routine called "PausedSection" which clears the timer, and runs whatever code you want it to.

提交回复
热议问题