views:

457

answers:

2

I have a function written in JScript (Not javascript) I need to suspend until a certain global variable becomes true. The global variable is changed to true when another function is called after an ajax response:

function(req, event, data) {
  globalVariable = true;
 }

When I try to loop until the variable is true:

while (globalVariable!= true) {
}

I go into a busy waiting and the callback function is never called.

Some suggested the use of WScript.wait() but my app doesn't seam to know WScript. SetTimeout() also won't help because it's asynchronic call and won't suspend my original function.

Any other suggestion?

Some more information regarding my question: I want my script to call 2 functions:

waitWhileAjaxIsNotCompleted();
doSomthingElse();

I want the waitWhileAjaxIsNotCompleted() to click a button that submits an ajax request (implemented by A4J) and terminate upon the ajax completion. In order for me to know when does tha ajax completed, I registered a function as a listener that will be awaken when the ajax completes. This function changes a globalVariable value. My waitWhileAjaxIsNotComplete() goes into an infinite loop, waiting for the glovalVariable value to change. When it does change (After the listener has awaken), I can end the function ad continue with the doSomthingElse() function.

You can see more on the implementation on: QTP Web extensibilty toolkit and ajax

A: 
baeltazor
Thanks. I tried it but when going into an infinite loop, no other function can be called to change the global variable to true. I need the wait() inside the infinite loop so other functions can be executed.
Eldad
This has got me stumped. Can you please provide a few more details regarding this issue?
baeltazor
I've edited the question to hold some more details regarding the issue.
Eldad
It's been quite a few days now since I've responded to this question, I've read the extra details you added the other day but still can't figure it out. I've still been searching around the web for a solution but it looks like nobody's tried this before...I'll keep searching as this is a very interesting question and I'll repost when/if I find a solution, or at least something that may be helpful.
baeltazor
Okay... I'm back again. Nothing to report. Have you tried speaking with a Jscript developer over at http://www.microsoft.com/connect ?They're pretty responsive over there... :-)
baeltazor
+1  A: 

This has been an issue for me as well. Doing a while overloads the processor. The only way I've found so far to solve is to use a state machine, call setTimeout and return.

E.g.

var state = 'unfinished';

function mainFunc()
{
 if(state=='unfinished')
 {
   setTimeout('mainFunc()',1000);
   return;
 }
 alert('The function executed')
}
Gergely Orosz
This won't hold the execution of the doSomethingElse() function.
Eldad
Yes... the doSomethingElse() should be placed after the alert() call.
Gergely Orosz