Vbscript SetTimeout()赢了';t执行该函数

Vbscript SetTimeout()赢了';t执行该函数,vbscript,settimeout,internet-explorer-11,Vbscript,Settimeout,Internet Explorer 11,这是我的代码片段: '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

这是我的代码片段:

'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
发生的情况是,有时没有触发
alert()
,我不知道为什么。我做了一些关于setTimeout()的研究,他们说如果计时器过期,只要有机会执行,就会触发setTimeout。我相信在调用
WaitForAnEvent()
之后,将有一个执行setTimeout的机会,但是 有时是有时不是。

更新-------------------------------------------------------------------------------

我读了很多关于
setTimeout
的文章,他们都说(简而言之)如果浏览器忙着做某事,它就不能被触发

现在:

  • 假设浏览器正在做某些事情(无限)是正确的吗 setTimeout找不到触发函数的可用时间
  • 在VBScript/Javascript中是否有方法检查IE(浏览器)当前是否正在执行诸如呈现文本或执行某些脚本之类的操作

尝试删除函数名周围的引号:

timeoutTimer=window.setTimeout(警报,2000)
  • 我认为您应该将函数名从
    alert
    更改为与浏览器公开的元素不冲突的名称(有一个
    window.alert()
    函数)。也许这将按原样工作(未经测试),但最好避免混淆

  • 将事件绑定到处理程序的正确语法是检索对函数的引用(此处重命名)

    window.setTimeout(GetRef(“showAlert”),2000)

  • 可能是因为我没有足够的信息,但我认为没有必要使用
    waitforanvent()
    函数。事情发生了。将函数绑定到事件上执行,并将在需要时调用事件处理程序的工作留给浏览器

仅为样本而编辑(根据先前的答案改编)

在这个HTA中,有五个事件被处理:开始按钮按下、停止按钮按下、退出按钮按下、时钟间隔和文件存在性检查

基本思想是不要让代码一直运行。浏览器具有控件,当事件发生时(按下按钮或达到间隔),处理事件的代码被调用并结束

<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
暗淡的timerID,timerFile
子窗口加载
window.resizeTo 600280
设置时钟定时器为真
timerFile=window.setInterval(GetRef(“CheckFilePresence”),1500)
端接头
子CheckFilePresence
Dim myFile
使用CreateObject(“Scripting.FileSystemObject”)
myFile=.BuildPath(.GetSpecialFolder(临时文件夹),“test.txt”)
如果.files存在(myFile),则
fileStatus.innerText=“找到文件[”&myFile&“]
其他的
fileStatus.innerText=“文件[”&myFile&“]不存在”
如果结束
以
端接头
子设置时钟定时器(已启用)
如果启用,则
timerID=window.setInterval(GetRef(“刷新时间”),1000)
刷新时间
其他的
窗口清除间隔(timerID)
timerID=空
如果结束
StartButton.disabled=已启用
StopButton.disabled=未启用
端接头
次刷新时间
CurrentTime.InnerHTML=FormatDateTime(现在,vbLongTime)
端接头
子出口程序
如果不是IsEmpty(timerID),则window.clearInterval(timerID)
如果不是IsEmpty(timerFile),则为window.clearInterval(timerFile)
window.close()
端接头






为什么要标记Javascript?它看起来不像Javascript。@jfriend00“没错,VBScript是服务器端代码。它有客户端版本,但只在IE中。它与JavaScript@jfriend00-很抱歉,但我认为
setTimeout
在vbscript和javascript中的工作方式相同,因此我也在javascript中标记我的问题。您好,尝试了您的解决方案,但没有成功。它给我和错误
类型不匹配的'alert'
我想alert可能是一个系统函数,你能试着把你的函数名改成别的吗?非常感谢你的回答。我现在就试试这个。但是关于第三个,我需要
waitforevent
精确地位于它所在的位置,因为它下面的所有脚本都依赖于它。很抱歉,我不能在我的问题中提到这一点。谢谢你的建议,但它在我的情况下不起作用。还有其他想法吗?@Thorax,如果您的
WaitForAnvent
正在运行,则不会调度事件。您应该将事件绑定到函数,并将调用事件处理程序的工作留给浏览器。如果您需要定期检查
waitforevent
中的内容,请使用
setInterval
。很抱歉,我没有真正理解您的意思,我对VBScript和HTA非常陌生。你能给我举个例子吗?你可以把它写在答案里。多谢各位much@Thorax然后,您可以修改C++代码,用<代码> PekMeime<代码>替换<代码> GETMeals>代码>,并尽快将控件返回给浏览器。然后创建一个间隔来调用消息检查操作。