English 中文(简体)
How to set delay in vbscript
原标题:
  • 时间:2009-11-13 12:57:20
  •  标签:
  • vbscript

How to set delay in vbscript?

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

问题回答

Work this end (XP).

Create a new file, call it test.vbs. Put this in it.

WScript.Sleep 1000
MsgBox "TEST"

Run it, notice the delay before the message box is shown.

Note, the number is in Milliseconds, so 1000 is 1 second.

If you re trying to simulate a sleep delay in VBScript but WScript is not available (eg: your script is called from Microsoft s BGInfo tool), then try the following approach.

The example below will delay until 10 seconds from the moment the instruction is processed:

Dim dteWait
dteWait = DateAdd("s", 10, Now())
Do Until (Now() > dteWait)
Loop

if it is VBScript, it should be

WScript.Sleep 100

If it is JavaScript

WScript.Sleep(100);

Time in milliseconds. WScript.Sleep 1000 results in a 1 second sleep.

The following line will make your script to sleep for 5 mins.

WScript.Sleep 5*60*1000

Note that the value passed to sleep call is in milli seconds.

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:

<script language = "VBScript">

    Dim dtmStartTime

    Sub Test
        dtmStartTime = Now 
        idTimer = window.setTimeout("PausedSection", 5000, "VBScript")
    End Sub

    Sub PausedSection
        Msgbox dtmStartTime & vbCrLf & Now
        window.clearTimeout(idTimer)
    End Sub

</script>

<body>
    <input id=runbutton  type="button" value="Run Button" onClick="Test">
</body>

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.

Here s another alternative:

Sub subSleep(strSeconds)   subSleep(2)
    Dim objShell
    Dim strCmd
    set objShell = CreateObject("wscript.Shell")
     objShell.Run cmdline,1,False

    strCmd = "%COMSPEC% /c ping -n " & strSeconds & " 127.0.0.1>nul"     
    objShell.Run strCmd,0,1 
End Sub 

Time of Sleep Function is in milliseconds (ms)

if you want 3 minutes, thats the way to do it:

WScript.Sleep(1000 * 60 * 3)

better use timer:

Sub wait (sec)
    dim temp
    temp=timer
    do while timer-temp<sec
    loop
end Sub

Here is my solution. Worked with script, which was ran by third party program with no WScript declared and no import allowed.

Function MySleep(milliseconds)
  set WScriptShell = CreateObject("WScript.Shell")
  WScriptShell.Run "Sleep -m " & milliseconds, 0, true
end Function

Update
Looks like Microsoft removed Sleep.exe from win 8, so this doesn t work in win 8 unless you put Sleep.exe in folder defined in %path%.

Here is an update to the solution provided by @user235218 that allows you to specify number of milliseconds you require.

Note: The -n option is the number of retries and the -w is the timeout in milliseconds for ping. I chose the 127.255.255.254 address because it is in the loopback range and ms windows doesn’t respond to it.

I also doubt this will provide millisecond accuracy but on another note i tried it in an application using the ms script control and whilst the built in sleep function locked up the interface this method didn t.

If somebody can provide an explanation for why this method didn t lock up the interface we could make this answer more complete. Both sleep functions where run in the user thread.

Const WshHide = 0
Const WAIT_ON_RETURN = True

Sub Sleep(ByVal ms)

   Dim shell  As WScript.Shell

   If Not IsNumeric(ms) Then _
       Exit Sub

   Set shell = CreateObject("WScript.Shell")

   Call shell.Run("%COMSPEC% /c ping -n 1 -w " & ms & " 127.255.255.254 > nul", WshHide, WAIT_ON_RETURN)

End Sub

As stated in this answer:

Application.Wait (Now + TimeValue("0:00:01"))

will wait for 1 second

WScript.Sleep 100

The following code should work properly.





相关问题
What approach should I use to test a VBScript?

I ve been asked to help out with a project which has made extensive use of VBScript to process a whole bunch of text files and generate certain outputs - sanitized files, SQL entries etc.. The script ...

Unable to call c# code from vbscript - ActiveX error

I am trying to call a method I have written in C# from VBScript. I have followed just about all of the instructions I can find on the web and am still having problems. Specifically I am getting Error:...

How to set delay in vbscript

How to set delay in vbscript? WScript.Sleep(100) does not work on Windows XP, Vista.

Using Classes in a Dictionary in Classic ASP

I usually do C# but have inherited a classic ASP project. I have defined a class: Class clsPayment Public Name End Class Set objPayment = New clsPayment objPayment.Name = "...

How to check which Operating System?

How can I check OS version in a batch file or through a vbs in an Windows 2k/2k3 environment ? You know ... Something like ... : "If winver Win2k then ... or if winver Win2k3 then ....

Problem casting field when querying spreadsheet

I am trying to query an .xls spreadsheet with VBScript, but I have run into an issue when trying to cast a field. I connect to the spreadsheet like this. Set objConnection = CreateObject("ADODB....

热门标签