Advanced Search

Results 1 to 4 of 4

Thread: Stopping Function execution temporarily

  1. #1
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default Stopping Function execution temporarily

    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:

    Code:
    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
    Last edited by codeexploiter; 05-05-2009 at 01:28 PM.

  2. #2
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    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.

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Quote Originally Posted by forum_amnesiac View Post
    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.

  4. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    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'.

    Code:
    var k;
    function foo1(){
       j=0;
    	while ( j<10000){
    		x=1;
    		j=j+1;
    	}
         k = 1000;
     }
    
    function foo2(){
       foo1();
    	if(k){
         return k;
       }
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •