Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / March 2006



Tip: Looking for answers? Try searching our database.

Help with setTimeout or setInterval

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cp - 31 Mar 2006 04:20 GMT
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.

Signature

cp

pegasusflightresources@gmail.com - 31 Mar 2006 05:38 GMT
The variable amount of attempts could be made by creating a while loop
where:
  while(x<=1)
  setTimeout(function; timelimit)
  x=x+1
Now, in order to have a blocking timeout, that would require basically
the opposite of setTimeout, so you might as well have an extremely
large if loop surrounding pretty much everything in order to have the
interval of 500 milliseconds blocking to be maintained.  You might do
something like this:
  if(checkLoadStatus!=1)
  some_time = 500
  setTimeout(function that contains this phrase; some_time)
  some_time=some_time+500
I hope that this works, as I am not a complete expert on JavaScript,
but it is my best attempt.

____________________________________

I remain your most humble and ob't sv't.

Patrick Reilly
1st Coy
Colonel Seth Warner's Regiment
RobG - 31 Mar 2006 06:34 GMT
cp said on 31/03/2006 1:20 PM AEST:
> 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,
[quoted text clipped - 4 lines]
> How would I make the following code pause, and keep checking several
> times before gracefully moving on?

You can't get JavaScript to 'pause'.  You can send it into an endless
loop until some specified time is reached or status is achieved, but for
the user that will closely resemble a denial of service attack.  Some
browsers will halt such processes after a short time anyway out of
respect for the user.

If you are using something like this:

  <URL:http://jibbering.com/2002/4/httprequest.html>

then when the file is loaded, your function can be called. You might
also be interested in the stuff here: <URL:http://www.ajaxtoolbox.com/>

[...]

Signature

Rob

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.