English 中文(简体)
一劳永逸地履行职务
原标题:Function that executes once and only when an element is fully loaded

我有一点困难,即要履行“once<>>> /em”的Script功能,而且只有当一个多功能元素完全装满时。 I ve 尝试了setIntervals,setimeouts,FOR loops,/code> statements,WHILE loops, et gotten no where.

我确实设法让它一劳永逸地工作,但由于我只能让它去工作,将职能推迟了2秒(这不错,因为它没有确切说明它需要多长时间),并迅速重新履行同样职能和职能,这同样是好的。

我只需要不断扫描该网页,以了解某一要素是否存在并具有内容(inner Abdulaziz != unfin,或某种),一旦装上(而且仅一次)然后停止扫描该页。

是否有人找到了这样做的方法? 此外,我还需要Java,而不是 j。

Thanks.

Original Code

function injectLink_Bridge(){
    setTimeout(function(){
        injectLink();
    }, 2000);
}

function injectLink(){
    var headerElement = document.getElementsByClassName("flex-module-header");

    if (headerElement.innerHTML == undefined) {
        console.log("Element doesn t exist. Restarting function");

        setTimeout(function(){
            injectLink_Bridge(); //I can t remember if the bridge is necessary or not
        }, 2000); 
    } else {
        console.log("Element exists. Injecting");

        setTimeout(function(){
            headerElement[1].innerHTML += "code" //Inject code into the second instance of the class-array
        }, 2000);
    }
}

Finished code

function injectLink(){
    var headerElement = document.getElementsByClassName("flex-module-header")[1]; //Get the second instance

    if(headerElement && headerElement.innerHTML != ""){
        console.log("Element exists and has content. Injecting code...");
        headerElement.innerHTML += "code"; //Currently revising, due to T.J. Crowder s side-note
    } else {
        console.log("Element doesn t exist or has no content. Refiring function...");
        setTimeout(injectLink, 250);
    }
}
最佳回答

http://www.ohchr.org。

getElementsByClassName returns a NodeList, not an element; NodeList doesn t have an innerHTML property. If you want the second matching element, use [1] to get it, and then test whether you got something back (or if you like you can use .length > 1 to check first, but NodeList is documented as returning null for indexes that don t exist). Also, you don t need a function wrapper around your existing function when passing it to setTimeout. So:

function injectLink(){
    var headerElement = document.getElementsByClassName("flex-module-header")[1];

    if (!headerElement) {
        console.log("Element doesn t exist. Restarting function");

        setTimeout(injectLink, 2000); 
    } else {
        console.log("Element exists. Injecting");

        // Do you really want a further two second delay?
        setTimeout(function(){
            headerElement.innerHTML += "code"; //Inject code into the second instance of the class-array
        }, 2000);
    }
}

Side note: Using .innerHTML += "markup"; is generally not a great way to append content to an element. Here s what the browser does when you do that:

  1. Spins through the element and any descendant elements it has building an HTML string to give to the JavaScript layer.
  2. Tears down all content from the element, which has the by-product of removing any event handlers on descendant elements.
  3. Parses the HTML string and builds new nodes and elements for it.

So a bit of work, and also the possibility of wiping out event handlers. If you re adding a new child element, look at using createElement and appendChild. If you re adding further text content, look at createTextNode and appendChild.


www.un.org/Depts/DGACM/index_spanish.htm

如果你给你一个内容<代码>id。 (尽管不论你如何重新找到这一要素,同一概念都是行之有效的),但你可以这样做:

(function() {
    setTimeout(check, 0);
    function check() {
        var elm = document.getElementById( theId );
        if (!elm) {
            setTimeout(check, 250); // Check again in a quarter second or so
            return;
        }

        // Use the element
    }
})();

在那里,我们几乎立即进行第一次检查,如果我们不找到这一要素,我们稍后将再次安排检查250米。 由于我们只安排下一次检查,如果前一次检查没有找到这一要素,我们只能花尽可能多的时间找到这一要素。

如果该元素没有<代码>id,则大概请您重新找到somehow<>em>(querySelector,querySelectorAll,等等),从而知道你是否发现并能够安排下一次检查。

也许值得对其加以限制,例如:

(function() {
    var start = new Date();
    setTimeout(check, 0);
    function check() {
        var elm = document.getElementById( theId );
        if (!elm) {
            if (new Date() - start > 5000) { // More than five seconds
                throw "Couldn t find the element";
            }
            setTimeout(check, 250); // Check again in a quarter second or so
            return;
        }

        // Use the element
    }
})();

......如果你改变你的超文本,而且这个内容不再存在,你就会发现测试错误,并提醒你更新你的代码。

问题回答

Edit:现在,你已经展示了你的法典,因此存在一些问题。

例如,getElementsByClassName(>回归一个阵列,以便你能够这样做:

var headerElement = document.getElementsByClassName("flex-module-header");
if (headerElement.innerHTML == undefined) 

你们必须这样做:

var headerElement = document.getElementsByClassName("flex-module-header");
if (!headerElement || headerElement.length == 0 || headerElement[0].innerHTML == undefined) 

准则之前的最初答案是:

如果你向我们展示了整个问题,我们也许会提出更令人费解的问题,但根据你对我们所说的话,我们现在能够建议的是模糊的武力方法:

(function() {
    var item, cnt = 0; 
    var pollTimer = setInterval(function() {
        ++cnt;
        if (!item) {
            // cache the item if found so future checks will be faster
            item = document.getElementById("testObject");
        }
        if (item && item.innerHTML != "") {
            // the item has innerHTML now, you can process it
            // with code here
            clearInterval(pollTimer);
        } else {
            if (cnt > 100) {
               // if not found after 100 checks (20 seconds)
               // then stop looking so we don t keep going forever
               clearInterval(pollTimer);
            }
        }
    }, 200);
}){};




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.