简体   繁体   English

在 JavaScript 中向事件侦听器添加计时器

[英]Adding a timer to an event listener in JavaScript

I'm having a little bit of trouble managing the load of sites in a Firefox extension.我在管理 Firefox 扩展中的站点负载时遇到了一些麻烦。

I'm using JavaScript and developing a Firefox extension to search a string in a list of websites of my own, to look for bad words.我正在使用 JavaScript 并开发 Firefox 扩展来搜索我自己的网站列表中的字符串,以查找坏词。 The problem is that I am searching the list one by one.问题是我正在一一搜索列表。 That means, I am loading a webpage and I add an event listener to DOMContentLoaded;这意味着,我正在加载一个网页并向 DOMContentLoaded 添加一个事件侦听器; when the DOM loads a function is called to search for the string.当 DOM 加载时调用 function 来搜索字符串。 But, if the page does not load (A network issue for example) the function is never called.但是,如果页面未加载(例如网络问题),则永远不会调用 function。

Is there a way to set a timer to an event listener?有没有办法为事件监听器设置计时器? If the event is not triggered in 5 seconds then do something.如果事件在 5 秒内没有被触发,那就做点什么。 Or if there is another way to do that please advice.或者,如果有其他方法可以做到这一点,请建议。 I minimized the code a lot but i think you can get the idea here.我将代码最小化了很多,但我认为你可以在这里得到这个想法。

function main_(){
            //do some stuff here and if needed call to open a new page
            waitForDOM();
}

function waitForDOM(){
            //if the list of pages is grather than 0 then
    //add the new tab here and create the event listener
    gBrowser.addEventListener('DOMContentLoaded',pageLoaded,true);
 }

function pageLoaded(aEvent) {
        //do the search here and calls the function waitForDOM to load a new page and a event listener
    waitForDOM();
}

I don't know anything about coding Firefox extensions, but in a general sense you can certainly set a timer to do something if your event handler isn't called within a certain time.我对编码 Firefox 扩展一无所知,但在一般意义上,如果您的事件处理程序在一定时间内没有被调用,您当然可以设置一个计时器来做某事。 Something like this:像这样的东西:

var eventTriggeredFlag = false;

function waitForDOM(){
  gBrowser.addEventListener('DOMContentLoaded',pageLoaded,true);
  setTimeout(function(){
               if (!eventTriggeredFlag) {
                  // time's up without the event: do something
               }
             }, 5000);
}

function pageLoaded(aEvent) {
  eventTriggeredFlag = true;
  // whatever else you want to do here...
}

If you need to keep track of multiple events with separate timers you'd obviously have to make that more complicated, perhaps with some closures or an array of events or something.如果您需要使用单独的计时器跟踪多个事件,您显然必须使其变得更加复杂,可能需要一些闭包或一系列事件或其他东西。

It is better to add a progress listener instead of using DOMContentLoaded .最好添加进度侦听器而不是使用DOMContentLoaded The method onStateChange of your progress listener will always be called with STATE_STOP and STATE_IS_WINDOW flags when the page finishes loading - regardless of whether there is an error or not.当页面完成加载时,将始终使用STATE_STOPSTATE_IS_WINDOW标志调用进度侦听器的onStateChange方法 - 无论是否存在错误。 You can call Components.isSuccessCode(aStatus) to see whether the page loaded successfully (note that this will only report network errors, HTTP errors like 404 will still count as success).可以调用Components.isSuccessCode(aStatus)来查看页面是否加载成功(注意这只会报网络错误,HTTP 错误如 404 仍会算成功)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM