I'm a Perl programmer by background, and JavaScript is dim second or
third choice. I have an AJAX application, and at one point in the code,
I need to be sure that an object has been created before I attach an
interface element to it. I've googled this subject, and none of the
results seemed to fit, or I'm too dense to put them into practice.
How would I make the following code pause, and keep checking several
times before gracefully moving on?
var gLoad = new Object();
/* gLoad will contain x number of elements in the format gLoad[key] =
value;
function someFunction() {
// various other statements to load and parse an Asynchronus request
.....
for ( var i in gLoad ) {
var isLoaded = checkLoadedStatus(i);
/* the part I need help with:
if checkLoadedStatus returns false, block for 500 miliseconds
and try again. Stop trying after x attemps .. */
// some function call that only happens if isLoaded = true;
createSomeInterFaceElements( i );
}
return;
}
function checkLoadedStatus( objid ) {
if ( document.getElementById ) {
if ( document.getElementById( objid ) ) {
return true;
}
else return false;
}
else alert('unsuppported option'); // and hope for the best
}
In Perl I would do something like (untested) :
my $waitcount = 0;
my $isLoaded = checkLoadedStatus( 'xxx' );
if ( ! $isLoaded ) {
while ( $waitcount < 3 ) {
sleep(1);
last if $isLoaded = checkLoadedStatus('xxx');
$waitcount ++;
}
if ( $isLoaded ) {
# isLoaded is true do something
}
}
else {
# isLoaded is true do something
}
sub checkLoadedStatus {
# some logic that returns 1 or 0
}
Any help is appreciated.
--
cp