468,512 Members | 1,504 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

need a sleep or wait function

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
Jul 23 '05 #1
4 9261
coolsti wrote:
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 !
Jul 23 '05 #2
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.

Jul 23 '05 #3
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.

--
Rob

Jul 23 '05 #4
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

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Ryan Spencer | last post: by
4 posts views Thread by Radioactive Man | last post: by
4 posts views Thread by Alexander Erlich | last post: by
5 posts views Thread by Parahat Melayev | last post: by
5 posts views Thread by Sinan Nalkaya | last post: by
11 posts views Thread by mark | last post: by
6 posts views Thread by stayit | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.