How to wait until an element exists?

后端 未结 19 2127
广开言路
广开言路 2020-11-22 09:14

I\'m working on an Extension in Chrome, and I\'m wondering: what\'s the best way to find out when an element comes into existence? Using plain javascript, with an interval t

19条回答
  •  爱一瞬间的悲伤
    2020-11-22 09:30

    I used this approach to wait for an element to appear so I can execute the other functions after that.

    Let's say doTheRestOfTheStuff(parameters) function should only be called after the element with ID the_Element_ID appears or finished loading, we can use,

    var existCondition = setInterval(function() {
     if ($('#the_Element_ID').length) {
        console.log("Exists!");
        clearInterval(existCondition);
        doTheRestOfTheStuff(parameters);
     }
    }, 100); // check every 100ms
    

提交回复
热议问题