Asynchronus calls and callbacks to Object orientated code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • foldface@yahoo.co.uk

    #1

    Asynchronus calls and callbacks to Object orientated code

    Hi
    This might be a bit tricky.
    I want to call a method of a class using the setTimeout code, ok, can do this
    like this:

    var instance = this;
    timerID = window.setTimeo ut( function() { instance.Method (); }, 1000);

    Now I want to call a method a number of times from with the class and when they
    have all finished I want a second method to be called, I don't want the methods
    themselves to be altered, i.e. I want this to be generic.

    Here was my idea:

    var timerCounter = 0
    function CallFunc(func, callbackFunc)
    {
    timerCounter--;
    if(timerCounter == 0)
    {
    callbackFunc()
    }
    }

    function DoAsyncRequest( func, callbackFunctio n)
    {
    timerCounter++;
    return window.setTimeo ut( CallFunc(func, callbackFunctio n), 1);
    }


    You would call this using something like:

    timerId = DoAsyncRequest( this.method(), this.method2()) ;

    How could I do this, tryed using eval to no avail. I just don't know the
    language well enough yet.
    Any ideas? Example code would be worth a million words please :-)

    Ta
    F
  • Michael Winter

    #2
    Re: Asynchronus calls and callbacks to Object orientated code

    On 12 Feb 2004 07:22:12 -0800, <foldface@yahoo .co.uk> wrote:

    [snip]
    [color=blue]
    > You would call this using something like:
    >
    > timerId = DoAsyncRequest( this.method(), this.method2()) ;
    >
    > How could I do this, tryed using eval to no avail. I just don't know
    > the
    > language well enough yet.
    > Any ideas? Example code would be worth a million words please :-)[/color]

    Try dropping the parentheses so you have:

    timerId = DoAsyncRequest( this.method, this.method2 );

    With your original example, you're calling method() and method2(), not
    passing a reference to them. The body of DoAsyncRequest will never receive
    anything about the two methods, only their return values, if any.

    If this doesn't help, you'll need to provide more context: a example that
    shows how it would be used in a webpage.

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • foldface@yahoo.co.uk

      #3
      Re: Asynchronus calls and callbacks to Object orientated code

      This works, classmethod is called the correct number of times by classmethod2
      is only called once, after they have all been called.
      Thanks for you help

      <script>

      function SomeClass()
      {
      this.ClassMetho d = SomeClassMethod
      this.ClassMetho d2 = SomeClassMethod 2
      }

      function SomeClassMethod ()
      {
      alert("method")
      }

      function SomeClassMethod 2()
      {
      alert("method2" )
      }

      //--------------------------------

      var timerCounter = 0
      function CallFunc(func, callbackFunc)
      {
      func()

      timerCounter--;
      if(timerCounter == 0)
      {
      callbackFunc()
      }
      }

      function DoAsyncRequest( func, callbackFunctio n)
      {
      timerCounter++;
      return window.setTimeo ut( function(){Call Func(func, callbackFunctio n)}, 500);
      }

      //timerId = DoAsyncRequest( this.method, this.method2);
      var someClass = new SomeClass;
      timerId = DoAsyncRequest( someClass.Class Method, someClass.Class Method2);
      timerId = DoAsyncRequest( someClass.Class Method, someClass.Class Method2);
      timerId = DoAsyncRequest( someClass.Class Method, someClass.Class Method2);
      timerId = DoAsyncRequest( someClass.Class Method, someClass.Class Method2);
      timerId = DoAsyncRequest( someClass.Class Method, someClass.Class Method2);
      timerId = DoAsyncRequest( someClass.Class Method, someClass.Class Method2);


      </script>

      Comment

      • Michael Winter

        #4
        Re: Asynchronus calls and callbacks to Object orientated code

        [This followup was posted to comp.lang.javas cript and a copy was sent to
        the cited author.]

        In article <acb840c8.04021 60212.7a816341@ posting.google. com>,
        foldface@yahoo. co.uk says...
        [color=blue]
        > This works, classmethod is called the correct number of times by classmethod2
        > is only called once, after they have all been called.
        > Thanks for you help[/color]

        [snip]

        You should review the thread I started on the 14th called "Exception
        when calling a DOM method using a reference to it". There are
        implications for object-oriented code, specifically when using the
        'this' operator, and passing method references. I didn't know about this
        at the time of my reply to you.

        Mike

        Comment

        Working...