WScript provides a function called Sleep. You can use sleep function to pause the script for specified milliseconds. Yeah! all good. But I wanted to pause the script for few minutes (sometimes more than 30 minutes). Specifing 15 minutes in milliseconds means 900000, 30 minutes means 1800000 milliseconds. I didn’t wan to deal with bigger numbers and didn’t feel right. So I wrote a small subroutine to pause the script by specified minutes. Here it is and feel free use it.
‘*********************************************************
‘ Sub Routine: GoToSleep
‘ Sub Routine: GoToSleep
‘ Parameters: Number of Minutes
‘ Purpose: Pause the script for specified minutes
‘ Written by: Anand Venkatachalapathy
‘ Date Written: July 1 2009
‘*********************************************************
Sub GoToSleep (iMinutes)
Dim Starting, Ending, t
Starting = Now
Ending = DateAdd("n",iMinutes,Starting)
Do
t = DateDiff("n",Now,Ending)
If t <= 0 Then Exit Do
WScript.Sleep 10000
Loop
End Sub
Dim Starting, Ending, t
Starting = Now
Ending = DateAdd("n",iMinutes,Starting)
Do
t = DateDiff("n",Now,Ending)
If t <= 0 Then Exit Do
WScript.Sleep 10000
Loop
End Sub
‘******* End of Sub Routine **************
If you really care how this subroutine works, read ahead.
1. Register the current time (Starting = Now)
2. Add the specified minutes to the current time and find the time after x minutes (Ending = DateAdd("n",iMinutes,Starting))
3. Do a "Do/Loop" loop till the current time is the "Time" after x minutes (see the Do Loop above).
Wow… Pretty useless script.Then lines where a simple WScript.Sleep 15 * 60 * 1000 could have sufficed for 15 minutes.
How to use an atomic bomb to kill a fly !!!!
Lol!! Not very efficient code, considering you are processing so many lines of code when it could be dnoe with just 1 line, on a slow machine you’ll probably draw out the sleep to 5 mins!
The other reason to use “Sleep” instead of a loop is that the script will stop using processor resources unless it has to service an event.
This is especially important if you are using an operating system that supports multi tasking. 😉