{"id":67643,"date":"2006-03-30T14:45:00","date_gmt":"2006-03-30T14:45:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/30\/how-can-i-temporarily-pause-a-script-in-an-hta\/"},"modified":"2006-03-30T14:45:00","modified_gmt":"2006-03-30T14:45:00","slug":"how-can-i-temporarily-pause-a-script-in-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-temporarily-pause-a-script-in-an-hta\/","title":{"rendered":"How Can I Temporarily Pause a Script in an HTA?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I temporarily pause a script in an HTA?<BR><BR>&#8212; TJ<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, TJ. You know, throughout history people have spent an enormous amount of time and energy searching for the meaning of life. The Scripting Guys have never taken part in this quest. Why not? Well, laziness aside, we know that it doesn\u2019t matter: even if you <I>did<\/I> find the meaning of life, no one would care. No one wants to know why we exist; instead, they &#8211; like you &#8211; just want to know how the heck they can pause a script embedded in an HTML Application (HTA). <I>That\u2019s<\/I> the cause the Scripting Guys have dedicated their lives to. <\/P>\n<P>Now, if you\u2019re not familiar with HTAs you\u2019re first thought will likely be, \u201cJust use Wscript.Sleep.\u201d (Incidentally, if you\u2019re <I>not<\/I> familiar with HTAs, you might want to wander over to the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Center<\/B><\/A>.) That\u2019s a good thought, but it won\u2019t work. Why not? That\u2019s because the Wscript object is a somewhat-unique object; for one thing, you can\u2019t actually create an instance of this object. Instead, the Wscript object is automatically provided for you any time you\u2019re running under Windows Script Host. And that\u2019s the problem right there: when you\u2019re running code within an HTA you are <I>not<\/I> running under Windows Script Host. Instead, you\u2019re running under the script host provided by Internet Explorer. Because you\u2019re not running under Windows Script Host you aren\u2019t automatically given access to the Wscript object, and because you can\u2019t create the Wscript object on your own there\u2019s no way to access Wscript.Sleep. You are &#8211; in highly-technical terms &#8211; hosed.<\/P>\n<P>This wouldn\u2018t be that big of a deal if Internet Explorer provided a method comparable to Wscript.Sleep; unfortunately, it doesn\u2019t. So is there some kind of crazy workaround we can use instead of a Sleep method? Did you even have to ask?<\/P><PRE class=\"codeSample\">&lt;script language = &#8220;VBScript&#8221;&gt;<\/p>\n<p>    Dim dtmStartTime<\/p>\n<p>    Sub Test\n        dtmStartTime = Now \n        idTimer = window.setTimeout(&#8220;PausedSection&#8221;, 5000, &#8220;VBScript&#8221;)\n    End Sub<\/p>\n<p>    Sub PausedSection\n        Msgbox dtmStartTime &amp; vbCrLf &amp; Now\n        window.clearTimeout(idTimer)\n    End Sub<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;input id=runbutton  type=&#8221;button&#8221; value=&#8221;Run Button&#8221; onClick=&#8221;Test&#8221;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>What we have is a very simple HTA: it consists entirely of a single button that, when clicked, runs a subroutine named Test. Big deal, you say? Well, let\u2019s take a look at the &lt;SCRIPT&gt; section of our HTA and decide how big a deal it really is.<\/P>\n<P>The first thing we do within the &lt;SCRIPT&gt; section is declare a global variable named dtmStartTime:<\/P><PRE class=\"codeSample\">Dim dtmStartTime\n<\/PRE>\n<P>This actually has nothing to do with pausing the script; instead it\u2019s just a variable we use to help us see that the script really <I>did<\/I> pause as expected. If that doesn\u2019t make any sense to you at the moment, just be patient: it should all become clear very soon.<\/P>\n<P>Next we have the Test subroutine, which gets called when we click the button:<\/P><PRE class=\"codeSample\">Sub Test\n    dtmStartTime = Now \n    idTimer = window.setTimeout(&#8220;PausedSection&#8221;, 5000, &#8220;VBScript&#8221;)\nEnd Sub\n<\/PRE>\n<P>Notice that we have just two lines of code in this subroutine. In the first line we simply assign the current date and time (using the <B>Now<\/B> function) to the variable dtmStartTime. That brings us to this line of code:<\/P><PRE class=\"codeSample\">idTimer = window.setTimeout(&#8220;PausedSection&#8221;, 5000, &#8220;VBScript&#8221;)\n<\/PRE>\n<P>Believe it or not, this is how we mimic the function of the Sleep method. What we\u2019re doing here is using the <B>setTimeout<\/B> method to create a timer, which we give the ID idTimer. As you can see, we pass setTimeout three parameters:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>PausedSection<\/B>. This is the name of the subroutine we want to run when the timer expires.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>5000<\/B>. This is the amount of time (in milliseconds; 5000 milliseconds equals 5 seconds) we want the timer to wait before calling PausedSection. In other words, that\u2019s our pause. If we want to pause the script for 30 seconds then we\u2019d set this parameter to 30000.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>VBScript<\/B>. A required parameter that simply lets the script know that PausedSection is written in VBScript.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>In other words, when we click the button the Test subroutine will run. When the Test subroutine runs, it creates a timer named idTimer. The sole function of idTimer is to wait 5 seconds and then call the subroutine PausedSection. Does that make sense? Good.<\/P>\n<P>So what do we do inside the subroutine PausedSection? Well, obviously we could run any code we wanted; for simplicity\u2019s sake we do only two things here. First, we display a message box showing the time we clicked the button and the time that the message box itself actually appeared on screen:<\/P><PRE class=\"codeSample\">Msgbox dtmStartTime &amp; vbCrLf &amp; Now\n<\/PRE>\n<P>Allowing for rounding errors, the difference between the time we clicked the button and the time the message box appeared should be 5 seconds. Is that important? You bet it is. Remember, displaying this message box is the first thing that happens inside the PausedSection subroutine. If it shows up 5 second after we click the button then that means we paused our script for 5 seconds. Which, as you might recall, was the whole idea in the first place.<\/P>\n<P>The other thing we do inside the PausedSection subroutine is call the <B>clearTimeout<\/B> method and effectively get rid of our timer:<\/P><PRE class=\"codeSample\">window.clearTimeout(idTimer)\n<\/PRE>\n<P>Why do we do that? That\u2019s easy: timers are designed to run forever. If we don\u2019t clear the timer then every 5 seconds idTimer will call the PausedSection subroutine, and every 5 seconds our little message box will pop up onscreen. We don\u2019t really want to see that message box every 5 seconds, so we use clearTimeout to remove the timer.<\/P>\n<P>Admittedly, this isn\u2019t quite as easy as simply saying Wscript.Sleep 5000; that means you\u2019ll probably have to think a little bit before you start enabling and disabling timers. In turn, that can be a bit of a challenge, but, hey, isn\u2019t that what life\u2019s all about?<\/P>\n<P>And no, that wasn\u2019t a rhetorical question. Like we said, we\u2019ve been busy trying to pause HTAs; beyond that we have no idea whatsoever what life is all about.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I temporarily pause a script in an HTA?&#8212; TJ Hey, TJ. You know, throughout history people have spent an enormous amount of time and energy searching for the meaning of life. The Scripting Guys have never taken part in this quest. Why not? Well, laziness aside, we know that it [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-67643","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I temporarily pause a script in an HTA?&#8212; TJ Hey, TJ. You know, throughout history people have spent an enormous amount of time and energy searching for the meaning of life. The Scripting Guys have never taken part in this quest. Why not? Well, laziness aside, we know that it [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67643"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67643\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}