This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need to pause javascript

 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Katie McCann
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disregard the previos post, it needs some tweaking for it to work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic