JackLeo JackLeo - 3 months ago 8
jQuery Question

While variable is not defined - wait

I have

click
event that is triggered from other place automatically for the first time. My problem is that it runs to soon, since 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 to fast and vice versa - for person with fast internet connection - its too damn slow.

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

Answer

I would prefer this code :

 function checkVariable() {

   if(variableLoaded==true){
    // here is your next action 
   }

 }

  setTimeout(checkVariable,1000);
Comments