473,670 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ifrmf orm.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 9505
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.ifrmf orm.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.c om/mangokun/programming/javascript/control_setTime out.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.ifrmf orm.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.ifrmf orm.flag.value) {
setTimeout("sav eFields()",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.getEl ementById) {
theDiv = document.getEle mentById('div1' )
} else if (document.all) {
theDiv = document.all['div1']
}

theDiv.innerHTM L='<...

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="savestrin g" 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($_SESSIO N['timedout']))
{
// Here, if timed out, set by code that each page request sees
unset($_SESSION['timedout']);
?>
<script>
parent.timeUser Out();
</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
5692
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". import time sleep(.5)
4
7693
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 = function_xyz("GIVE ME AN ANSWER: ", time_limit) The problem with "raw_input" is that it will stop unattended script indefinitely. I'm looking for a function that does the exact same thing, but with a time limit feature, and preferably one that returns
4
19385
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 after another_, returns always the same result: function rndnumber () {...} number1 = rndnumber();
5
23353
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 solution but it is not how it must be... i think. When threads working without sleep(1) I can't get response from server but when I put sleep(1) in thread function as you will see in code, everything works fine and server can make echo nearly for...
3
2114
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 "WaitSleepJoin" when it runs because the function it calls - "GenerateEvents()" has "Sleep()" function. Can I solve this problem with Thread? I don't want add nasty boll flags on it.
5
3101
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 i used time.sleep in while, the function in while never executed. then i found something about Timer but couldnt realize any algorithm in my mind.
11
1938
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 go back to sleep. How to do this? thanks mark class eventhndler(threading.Thread): def __init__(self):
4
2685
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 while I check internet connectivity for 30 secs but if I put the thread to sleep the connection itself closes. The current method is _sleepTimeBetweenConnectTries = 900 'milliseconds System.Threading.Thread.Sleep(_sleepTimeBetweenConnectTries)...
6
2172
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 your solution further so the program accepts an integer as a command line argument. The program then creates that number of child processes and assigns each child a ranodm number of seconds to sleep. Finally, the parent processes report as each child...
0
8468
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8660
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4390
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1792
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.