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?
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With