aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Need to pause javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Need to pause javascript" Watch "Need to pause javascript" New topic
Author

Need to pause javascript

Katie McCann
Ranch Hand

Joined: Jul 24, 2000
Posts: 45

Ok, I am desperate. Essentially, I need to be able to do this:
while (_myVar ==null)
{
sleep(100);
}
Basically, I don't want the current function to continue until the _myVar var has been set by another function (that is called by a DHTML layer that contains a form where the user makes a choice). I am aware that there is no sleep function in javascript, and I've tried working with setTimeOut() like this, but it doesn't work, as it seems that setTimeOut launches a new thread, so the function exits anyway:
function wait()
{
while (_userSelection ==null)
{
setTimeOut('wait();', 100);
}
//each wait call exits here anyway
}
Any ideas? Greatly appreciated!
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Katie
The call to setTime out is caling your wait function again so it almost like a recursive function. Try calling a different function that returns your _userSelection value. Something like this:
[code]
function wait(){
do{
setTimeOut('getUserSelection(_userSelection);', 100);
}while (_userSelection == null);
}
I think that'll do it for you.
Dave

Dave
Katie McCann
Ranch Hand

Joined: Jul 24, 2000
Posts: 45
Hm... that's not working either. What i did was this:
function wait()
{
while (_userSelection==null)
{
getUserSelection();
alert("calling set Timeout");
setTimeout('getUserSelection()', 1000000);
}
}
function getUserSelection()
{
return _userSelection;
}

Even though I put a huge number of ms in the timeout argument, it doesn't seem to be working right- the alerts are popping up constantly (i threw them in there to see what was going on), and the function is hogging up all my CPU and eventually crashing the browser.
Any other ideas would be very much appreciated. Thanks!
Katie McCann
Ranch Hand

Joined: Jul 24, 2000
Posts: 45

OK, I think I understand why the above doesn't work. According to some documentation I found:
" Note that the use of this method does not halt the execution of any remaining scripts until the timeout has passed, it just schedules the expression or function for the specified time."
So the while loop keeps executing continually, not ever 1000 ms or whatever.
I'm getting skeptical if this is even going to be possible. Anyone know a workaround?
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Katie
If the only thing the function does is check the value of the variable then do some processing if it isn't null then you can use setTimeout to rerun the function itself after a certain amount of time has passed.

What is the event that is going to happen that wil change _userSelection ? is it a user action in form? If so, can't you use the onchange event for that form item?
Dave

[This message has been edited by Dave Vick (edited November 09, 2001).]
Shawn Reynolds
Greenhorn

Joined: Dec 23, 2002
Posts: 2
I'm pretty sure this should do what you need.
var x = null;
function blah1(x){
if(x==null){
x = getUserSelection();
setTimeout('blah1(x);',1000);
}
}

function getUserSelection(){
//check for user selection - return null if nothing is selected.
return _userSelection;
}
Shawn Reynolds
Greenhorn

Joined: Dec 23, 2002
Posts: 2
Disregard the previos post, it needs some tweaking for it to work.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Need to pause javascript
 
Similar Threads
How to implement "real" JavaScript Sleep() or Wait()
How to delay response and than forward to another page?
Redirect
wait/sleep in javascript does not work
What does IE 8 Cache?