SetTimeout()将不执行该功能 [英] SetTimeout() won't execute the function

查看:138
本文介绍了SetTimeout()将不执行该功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码段:

'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的可用机会,但是 有时是,有时不是.

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 -------------------------------------------------------------------------------

我已经阅读了很多有关setTimeout的文章,他们都(简而言之)说,如果浏览器忙于做某事,就无法触发它.

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.

现在:

  • 假设浏览器正在执行某项操作(无限)是否正确,并且 setTimeout找不到可用的时间来触发该功能?
  • VBScript/Javascript中是否有一种方法可以检查IE(浏览器)当前是否正在执行诸如呈现文本或执行某些脚本之类的事情?
  • 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?

推荐答案

  • 我认为您应该将函数名称从alert更改为不与浏览器公开的元素冲突的功能(存在window.alert()函数).也许这可以按原样进行(未经测试),但是最好避免混淆

    • 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)

      可能是因为我没有足够的信息,但是我看不到需要您的WaitForAnEvent()函数.事件发生了.您将函数绑定为在事件上执行,然后将需要的工作留给浏览器以在需要时调用事件处理程序

      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)

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

      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>
      

      这篇关于SetTimeout()将不执行该功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆