473,394 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help with setTimeout or setInterval

cp
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
Mar 31 '06 #1
2 1554
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

Mar 31 '06 #2
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,
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?


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/>
[...]

--
Rob
Mar 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert Mark Bram | last post by:
Howdy All! I would like to find since when did different versions of browsers and/or JavaScript/JScript/ECMAScript support the SetTimeout and SetInterval methods.. this is for compatibility...
6
by: Robert Mark Bram | last post by:
Howdy All! How can I make something like this work? var message = "This will appear in 10 seconds"; setTimeout ('alert(message)', 10000); Thanks for any advice!
1
by: Weston C | last post by:
In the course of trying to build a simple clock, I've run into a problem using the setInterval (and setTimeout) function. http://weston.canncentral.org/misc/tkeep/tkeep.html...
1
by: John | last post by:
Michael Winter <M.Winter@blueyonder.co.invalid> wrote in message news:<opr3wd8yvj5vklcq@news-text.blueyonder.co.uk>... > On 24 Feb 2004 14:13:47 -0800, John <johnmark@fastermail.com> wrote: > > >...
5
by: oliver | last post by:
Hi, the structure of my source code is like this: <script> C = function(){ //some initialization } C.prototype.start = function{ //some action
18
by: Frances Del Rio | last post by:
this code is supposed to flip imgs at intervals of one second, but it flipps them much faster, it seems it flips them all in that first second, the more seconds the faster it flips them, instead of...
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
15
by: nikki_herring | last post by:
I am using setTimeout( ) to continuously call a function that randomly rotates/displays 2 images on a page. The part I need help with is the second image should rotate 3 seconds after the first...
2
by: shawnwperkins | last post by:
Hi Folks, I'm new to Javascript and just need a little help. I downloaded a script from Dynamic Drive's Web site and I'm trying to make a simple modification and could use some help. :) The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.