473,395 Members | 2,798 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,395 software developers and data experts.

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 9485
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Ryan Spencer | last post by:
Hello Everyone, I want to have a row of periods, separated by small, say, .5 second intervals between each other. Thus, for example, making it have the appearance of a progress "bar". ...
4
by: Radioactive Man | last post by:
anyone know of a function like "raw_input", which collects a string from the user entry, but one where I can set a time limit, as follows: time_limit = 10 # seconds user_answer =...
4
by: Alexander Erlich | last post by:
Hello, I have written a function that returns a random number in dependence of the time running on the current system. The problem is that initializing two variables with this function _one...
5
by: Parahat Melayev | last post by:
I am trying to writa a multi-client & multi-threaded TCP server. There is a thread pool. Each thread in the pool will handle requests of multiple clients. But here I have a problem. I find a...
3
by: kiplring | last post by:
Suppose a function which has Sleep() method in it. And I want to recycle it. I made two buttons which call "Resume()" and "Suspend()". But It doesn't work. The state of thread "t" will be...
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
11
by: mark | last post by:
Right now I have a thread that sleeps for sometime and check if an event has happened and go back to sleep. Now instead I want the thread to sleep until the event has occured process the event and...
4
Cintury
by: Cintury | last post by:
Hi All, I'm developing a mobile app in VB and I was wondering if there was anyway to pause or wait a thread without putting it to sleep. I'm just trying to maintain a wireless network connection...
6
by: stayit | last post by:
Expand the code so that the parent creates two children and then waits until both children and then waits until both children exit. Note: The code might have something to do in UNIX. Expand...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.