这是我的代码片段:
'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,但是有时是 and 有时并非如此。
Update-------------------------------------------------- ----------------------------
我读了很多关于setTimeout
他们都说(简而言之)如果浏览器正忙于做某事,则无法触发它。
Now:
- 假设浏览器正在做某事(无限)是否正确,以及
setTimeout 找不到可用时刻来触发该函数?
- VBScript/Javascript 有没有办法检查 IE(浏览器)当前是否正在执行渲染文本或执行某些脚本之类的操作?
-
我认为你应该更改你的函数名称alert
不与浏览器暴露的元素发生冲突的东西(有一个window.alert()
功能)。也许这会按原样工作(未经测试),但最好避免混淆
-
将事件绑定到处理程序的正确语法是检索对函数的引用(此处重命名)
window.setTimeout(GetRef("showAlert"), 2000)
-
可能是因为我没有足够的信息,但我不认为你需要WaitForAnEvent()
功能。事件发生。您将函数绑定为在事件上执行,并在需要时将调用事件处理程序的工作留给浏览器
edited仅作为示例(改编自之前的答案)
在此 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)