PDA

View Full Version : Resolved Stopping Function execution temporarily



codeexploiter
05-04-2009, 03:13 PM
Hi all,

I am looking for a way to stop a function's normal execution temporarily. The scenario is something like this:

I have a function that needs to return something to its caller and this can't be changed. This function used to call another function before starting its execution and needs to wait until the other function completes. Once the other function completes this function can start its execution.

Ex:



var k;
function foo1(){
setTimeout(function(){
k = 1000;
}, 10000);
}

function foo2(){
foo1();
if(k){
return k;
}
}

foo2();


As you know this will return undefined from foo2. What I want to do is the return statement should be executed only after the complete execution of foo1 function.

In my case I am dealing with Ajax calls but the issue here is the location of the return statement, which I can't change.

I will not be able to use the setTimeout/setInterval...

Thanks

forum_amnesiac
05-04-2009, 03:32 PM
Just a suggestion from a novice.

In your example is it not possible to have an if or while statement that tests for the value of k. When k has been initialised/defined you could then do the return.

codeexploiter
05-04-2009, 04:00 PM
Just a suggestion from a novice.

In your example is it not possible to have an if or while statement that tests for the value of k. When k has been initialised/defined you could then do the return.

This will not make much difference. Even after doing this It will return 'undefined' from foo2 function.

forum_amnesiac
05-04-2009, 05:17 PM
I'm not sure if this gives the same functionality as you want but it does set an adjustable delay into 'foo1', like the timeout, and does return the value of 'k' into 'foo2'.


var k;
function foo1(){
j=0;
while ( j<10000){
x=1;
j=j+1;
}
k = 1000;
}

function foo2(){
foo1();
if(k){
return k;
}
}