1var checkExist = setInterval(function() {
2 if ($('#the-canvas').length) {
3 console.log("Exists!");
4 clearInterval(checkExist);
5 }
6}, 100); // check every 100ms
7
1
2//https://gist.github.com/chrisjhoughton/7890303
3
4var waitForEl = function(selector, callback) {
5 if (jQuery(selector).length) {
6 callback();
7 } else {
8 setTimeout(function() {
9 waitForEl(selector, callback);
10 }, 100);
11 }
12};
13
14waitForEl(selector, function() {
15 // work the magic
16});
17
18
19
20
1// Call the below function
2waitForElementToDisplay("#div1",function(){alert("Hi");},1000,9000);
3
4function waitForElementToDisplay(selector, callback, checkFrequencyInMs, timeoutInMs) {
5 var startTimeInMs = Date.now();
6 (function loopSearch() {
7 if (document.querySelector(selector) != null) {
8 callback();
9 return;
10 }
11 else {
12 setTimeout(function () {
13 if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs)
14 return;
15 loopSearch();
16 }, checkFrequencyInMs);
17 }
18 })();
19}
20