Ask:
Hello, Scripting Guy! How do I pause a script in an HTA?
--TJ
For:
Hello, TJ. You know, from the ancient times, people have spent a great deal of time and energy to explore the meaning of life. The Scripting Guys have never been involved. Why? Well, let's not say lazy, we know it doesn't matter: even if you did find the meaning of life, no one cares about it. No one wants to know why we exist; instead, they-like you-want to know exactly how to suspend a script embedded in an HTML application (HTA). That's what the Scripting Guys are dedicated to.
Now, if you are unfamiliar with HTA, the first thing you can think of is "use Wscript.Sleep". (Incidentally, if you're not familiar with an HTA, you can browse the HTA developers Center.) This is a good idea, but it can't be achieved. Why? Because the Wscript object is a slightly unique object, one reason is that you are not actually able to create an instance of this object. As long as you are running under Windows Script Host, Wscript is automatically available to you. Here's the problem: When you run code inside your HTA, you're not running under Windows Script Host. You are actually running under the script host provided by Internet Explorer. Because you are not running under Windows Script Host, you cannot automatically access the Wscript object, and you cannot access wscript.sleep because you cannot create your own Wscript object. In high-tech terms, it's "paralyzed."
This is not a big deal if Internet Explorer provides a method similar to Wscript.Sleep; Unfortunately, it is not provided. So is there some kind of magical solution that we can use instead of the sleep method? Is it necessary to ask?
<script language = "VBScript" > Dim dtmstarttime Sub Test dtmstarttime = Now Idtimer = Window.settimeout ("PausedSection", 5000, "VBScript") End Sub sub pausedsection Msgbox Dtmstarttim E & vbCrLf & Now Window.cleartimeout (idtimer) End Sub </script> <body> <i Nput Id=runbutton type= "button" value= "Run button" onclick= "Test" > </body>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Our HTA is very simple: it consists of only one button, and when clicked, a subroutine named Test is run. It's great, isn't it? OK, let's look at the <SCRIPT> part of the HTA and then make sure it's really great.
The first thing we do in <SCRIPT> is to declare a global variable named dtmStartTime:
Dim dtmStartTime
This actually has no effect on pausing the script; we just use this variable to help us see if the script really pauses as expected. If you don't understand what it means now, don't worry: it'll be clear soon.
Next is the Test subroutine, which is called when we click the button:
Sub Test
dtmStartTime = Now
Idtimer = Window.settimeout ("PausedSection", 5000, "VBScript")
End Sub
Note that there are only two lines of code in this subroutine. In the first row, we just give the variable dtmstarttime the current time and date (using the Now function). Next is the following line of code:
Idtimer = Window.settimeout ("PausedSection", 5000, "VBScript")
Whether you believe it or not, this is how we implement the Sleep method function. We used the SetTimeout method to create a timer with an ID of Idtimer. As you can see, we passed three parameters to settimeout:
? PausedSection. This parameter is the name of the subroutine we want to run after the timer comes.
? 5000. This parameter is the time that we want the timer to wait before calling PausedSection (in milliseconds, 5000 milliseconds equals 5 seconds). In other words, this is the pause we need. If you need to pause the script for 30 seconds, you can set this parameter to 30000.
? VBScript. This parameter is required, and it simply tells the script that the PausedSection was written in VBScript.
That is, when we click the button, the Test subroutine runs. When the Test subroutine runs, it creates a timer named Idtimer. The only function of the idtimer is to wait 5 seconds and then invoke the subroutine PausedSection. Do you understand? Good.
So what does subroutine pausedsection do? Obviously, we can run any desired code, and for simplicity's sake, we do only two things here. First, display a message box that shows when we clicked the button and when the message box itself actually appears on the screen:
Msgbox dtmStartTime & VbCrLf & Now
If rounding errors are allowed, the interval between these two times should be 5 seconds. Does that matter? Of course it's important. Keep in mind that the first thing that happens inside the PausedSection subroutine is to display this message box. If this message box is displayed 5 seconds after we click the button, our script is paused for 5 seconds. You may remember that this is our ultimate goal.
Another thing we need to do in the PausedSection subroutine is to call the Cleartimeout method to effectively purge the timer:
Window.cleartimeout (Idtimer)
Why did you do that? The reason is simple: the timer is designed to run forever. If you do not clear the timer, every 5 seconds Idtimer will call the PausedSection subroutine once, so every 5 seconds our small message box will pop up on the screen. We really don't want to see such a message box every 5 seconds, so use cleartimeout to remove the timer.
We acknowledge that this is not as simple as using Wscript.Sleep 5000, which means you might have to weigh a little before you enable and disable timers. So this may be a bit of a challenge, but don't forget, isn't that the whole point of life?
No, it's not a rhetorical question. As we've said, we've been working on halting the HTA, and beyond that, we don't know what life is.