need a sleep or wait function

C

coolsti

I am aware of the setInterval and setTimeout functions for Javascript, but
I can't seem to find an example that lets me do what I need to.

I have a script that needs to wait until a certain condition is met. In
this case, the condition is that an iframe has been reloaded completely. I
do this by examining a hidden document variable in the iframe, and when it
has changed from "" to "1" is the signal for me that the iframe is loaded.

(if there is an easier way to check this, please let me know. I am
otherwise not using frames, just one iframe).

What I need to do can be seen with this example function:

function saveFields() {
var str ;
if ( window.frames['ifrm'] ) {
while (window.frames['ifrm'].document.ifrmform.flag.value == "") {
// loop here until condition is met
}
// rest of code which should be performed AFTER condition is met
}
}

If I just let this run in my browser, the browser crashes, and I suppose
it is because all of its allotted CPU time is being used to check the
empty while loop. So I need a sleep function here, which will cause the
checking to wait, say, half a second each time. All I can see from the
setTimeout functions is that the function which calls them returns right
away, so I don't see how I can use that.

Any suggestions?

Thanks!
Steve, Denmark
 
@

@(none)

coolsti said:
I am aware of the setInterval and setTimeout functions for Javascript, but
I can't seem to find an example that lets me do what I need to.

I have a script that needs to wait until a certain condition is met. In
this case, the condition is that an iframe has been reloaded completely. I
do this by examining a hidden document variable in the iframe, and when it
has changed from "" to "1" is the signal for me that the iframe is loaded.

(if there is an easier way to check this, please let me know. I am
otherwise not using frames, just one iframe).

What I need to do can be seen with this example function:

function saveFields() {
var str ;
if ( window.frames['ifrm'] ) {
while (window.frames['ifrm'].document.ifrmform.flag.value == "") {
// loop here until condition is met
}
// rest of code which should be performed AFTER condition is met
}
}

If I just let this run in my browser, the browser crashes, and I suppose
it is because all of its allotted CPU time is being used to check the
empty while loop. So I need a sleep function here, which will cause the
checking to wait, say, half a second each time. All I can see from the
setTimeout functions is that the function which calls them returns right
away, so I don't see how I can use that.

Any suggestions?

Thanks!
Steve, Denmark

Look at the page source for
"http://www.geocities.com/mangokun/programming/javascript/control_setTimeout.htm"

I am not 100% sure whether this gives up the CPU while it times - but I
think it does ! Anyone who writes a timeout that doesn't isn't very clever !
 
L

Lee

coolsti said:
I am aware of the setInterval and setTimeout functions for Javascript, but
I can't seem to find an example that lets me do what I need to.

I have a script that needs to wait until a certain condition is met. In
this case, the condition is that an iframe has been reloaded completely. I
do this by examining a hidden document variable in the iframe, and when it
has changed from "" to "1" is the signal for me that the iframe is loaded.

(if there is an easier way to check this, please let me know. I am
otherwise not using frames, just one iframe).

What I need to do can be seen with this example function:

function saveFields() {
var str ;
if ( window.frames['ifrm'] ) {
while (window.frames['ifrm'].document.ifrmform.flag.value == "") {
// loop here until condition is met
}
// rest of code which should be performed AFTER condition is met
}
}

If I just let this run in my browser, the browser crashes, and I suppose
it is because all of its allotted CPU time is being used to check the
empty while loop. So I need a sleep function here, which will cause the
checking to wait, say, half a second each time. All I can see from the
setTimeout functions is that the function which calls them returns right
away, so I don't see how I can use that.

Any suggestions?

If you want a delay, use setTimeout().
Don't try something else just because you don't know how to use setTimeout().
The following version of saveFields() will reschedule itself to execute every
half second until the flag is set:

function saveFields() {
var str ;
if ( window.frames['ifrm'] ) {
if (!window.frames['ifrm'].document.ifrmform.flag.value) {
setTimeout("saveFields()",500);
} else {
// rest of code which should be performed AFTER condition is met
}
}
}

However, rather than having this function ask "are you done yet? are you done
yet?" over and over, you might consider having the iframe's onLoad handler
trigger whatever action should be taken when it loads.
 
R

RobG

none wrote:
[...]
Look at the page source for
[...]

No, don't - use Lee's setTimeout() suggestion below. The link proided
by "none" is IE only (and maybe some versions of Opera):

div1.innerHTML='<...

Referencing DOM objects doesn't work in most non-IE browsers. The
equivalent, cross-browser solution would be:

var theDiv;
if (document.getElementById) {
theDiv = document.getElementById('div1')
} else if (document.all) {
theDiv = document.all['div1']
}

theDiv.innerHTML='<...

And if you want to support Netscape 4.x, look at document.layers too.
I am not 100% sure whether this gives up the CPU while it times - but I

Yes, it should because it uses setTimeout() and checks back once each
second.
 
C

coolsti

Hi,

thanks for the various suggestions to the posters.

Actually, I solved the problem another way. I instead have the iframe
code do the work as soon as it is loaded up, so I no longer need my wait
function.

In my script which is called by the iframe:

<form name="ifrmform" method="post"
action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="savestring" value="" >
<input type="hidden" name="action" value="" >
</form>
<?php
if (trim($_POST['action']) == 'autosave')
{
// Code here to connect to database and update (save) variables
if (isset($_SESSION['timedout']))
{
// Here, if timed out, set by code that each page request sees
unset($_SESSION['timedout']);
?>
<script>
parent.timeUserOut();
</script>
<?php
}
}
?>

and in the main document script:

<script>
function timeUserOut() {
packValues(); // prepare for a timed out situation
document.form1.submit(); // auto-submit the form
}
</script>

In other words, instead of trying to check in the main document for when
the iframe is fully loaded, I use the iframe itself to initiate the
action, which of course occurs first after the iframe is fully loaded.

In this case, the iframe serves as an auto-save but my pages are also
protected by a time-out; if the user doesn't request another page
within the time out period, the user is asked to log in again at the next
page request, with all
posted variables being stored in a session variable until the user logs in
again. But with this page, the user is working via Javascript mostly
on the client side for long periods of time (hence the need for
autosave) and so the time out idea wouldn't work if the autosave went
into action. So now, when the auto-save notices a time-out, it will
call the parent JS function to pack up all the user's form variables and
force a submit to refresh the page - and since the user timed out, he/she
will be confronted with the log in page.

Too bad there isn't a simple sleep function in JS.

/Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top