Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While variable is not defined - wait

I have a click event that is triggered from another place automatically for the first time. My problem is that it runs too soon, since the required variables are still being defined by Flash and web services. So right now I have:

(function ($) {     $(window).load(function(){         setTimeout(function(){             $('a.play').trigger("click");         }, 5000);     }); })(jQuery); 

The problem is that 5 seconds for a person with a slow internet connection could be too fast and vice versa, for a person with a fast internet connection, it's too slow.

So how should I do the delay or timeout until someVariable is defined?

like image 713
JackLeo Avatar asked Sep 05 '11 12:09

JackLeo


1 Answers

The following will keep looking for someVariable until it is found. It checks every 0.25 seconds.

function waitForElement(){     if(typeof someVariable !== "undefined"){         //variable exists, do what you want     }     else{         setTimeout(waitForElement, 250);     } } 
like image 69
dnuttle Avatar answered Sep 27 '22 23:09

dnuttle