SetTimeout() won't execute the function

2020-07-25 01:34发布

问题:

So this is my code snippet:

'in VBScript

Sub Main()
   Dim timeoutTimer

   'more scripts here
   'more scripts here
   'more scripts here

   timeoutTimer = window.setTimeout("alert()", 2000)

   Call WaitForAnEvent() 'This function waits for an event to happen
                         'if there is no event then code execution stop's
                         'and wait
   'more scripts here
   'more scripts here
   'more scripts here
End Sub 

Sub alert()
   MsgBox "Help!"
End Sub

What happens is, there are times when alert() is not triggered, and I don't have any idea why. I conducted some research about setTimeout() and they said that setTimeout will be triggered if the timer expires and as soon as there is an available opportunity to execute it. I believe after WaitForAnEvent() is invoked there will be an available opportunity for setTimeout to be executed but sometimes it is and sometimes it is not.

Update -------------------------------------------------------------------------------

I had been reading a lot of articles about setTimeout and they all say(in short) that it cannot be triggered if the browser is busy doing something.

Now:

  • Is it correct to assume that the browser is doing something(infinite), and setTimeout cannot find an available moment to trigger the function?
  • Is there a way in VBScript/Javascript to check if IE(browser) is currently doing somthing like rendering text or executing some scripts?

回答1:

  • I think you should change your function name from alert to something that does not collide with elements exposed by the browser (there is a window.alert() function). Maybe this will work as is (not tested), but it is better to avoid confusion

  • The proper syntax to bind the event to the handler is to retrieve a reference to the function (here renamed)

    window.setTimeout(GetRef("showAlert"), 2000)

  • Probably because I don't have enough information, but I don't see the need for your WaitForAnEvent() function. Events happen. You bind the function to execute on event and leave to the browser the work to call the event handler when needed

edited Just for a sample (adapted from a previous answer)

In this HTA, there are five events being handled: Start button press, Stop button press, Exit button press, Clock interval and File existence check

The basic idea is NOT to have code running all the time. The browser has the control and when an event happens (button pressed or interval reached) the code to handle the event is called and ends.

<html>
<head>
<title>ClockwithAlerts</title>
<HTA:APPLICATION 
    ID="ClockHTA"
    APPLICATIONNAME="ClockHTA"
    MINIMIZEBUTTON="no"
    MAXIMIZEBUTTON="no"
    SINGLEINSTANCE="no"
    SysMenu="no"
    BORDER="thin"
/>

<SCRIPT LANGUAGE="VBScript">

Const TemporaryFolder = 2

Dim timerID, timerFile

Sub Window_onLoad
    window.resizeTo 600,280
    SetClockTimer True 
    timerFile = window.setInterval(GetRef("CheckFilePresence"), 1500)
End Sub

Sub CheckFilePresence
    Dim myFile
    With CreateObject("Scripting.FileSystemObject")
        myFile = .BuildPath(.GetSpecialFolder( TemporaryFolder ), "test.txt")
        If .FileExists(myFile) Then 
            fileStatus.innerText = "FILE ["& myFile &"] FOUND"
        Else
            fileStatus.innerText = "File ["& myFile &"] is not present"
        End If
    End With
End Sub 

Sub SetClockTimer( Enabled )
    If Enabled Then 
        timerID = window.setInterval(GetRef("RefreshTime"), 1000)
        RefreshTime
    Else 
        window.clearInterval(timerID)
        timerID = Empty 
    End If
    StartButton.disabled = Enabled
    StopButton.disabled = Not Enabled
End Sub

Sub RefreshTime
    CurrentTime.InnerHTML = FormatDateTime(Now, vbLongTime)
End Sub

Sub ExitProgram
    If Not IsEmpty(timerID) Then window.clearInterval(timerID)
    If Not IsEmpty(timerFile) Then window.clearInterval(timerFile)
    window.close()
End Sub

</SCRIPT>

</head>

<body>
    <input id="checkButton" type="button" value="EXIT" name="run_button" onClick="ExitProgram" align="right">
<br><br>
    <span id="CurrentTime"></span>
<br><br>
    <input id="Stopbutton"  type="button" value="Stop"  name="StopButton"  onclick="SetClockTimer(False)">
    <input id="StartButton" type="button" value="Start" name="StartButton" onclick="SetClockTimer(True)">
    <hr>
    <span id="fileStatus"></span>
</body>
</html>


回答2:

try remove quotes around your function name:

timeoutTimer = window.setTimeout(alert, 2000)