Asynchronus calls and callbacks to Object orientated code

Discussion in 'Javascript' started by foldface@yahoo.co.uk, Feb 12, 2004.

  1. Guest

    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.setTimeout( 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, callbackFunction)
    {
    timerCounter++;
    return window.setTimeout( CallFunc(func, callbackFunction), 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
     
    , Feb 12, 2004
    #1
    1. Advertising

  2. On 12 Feb 2004 07:22:12 -0800, <> wrote:

    [snip]

    > 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 :)


    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
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Feb 12, 2004
    #2
    1. Advertising

  3. Guest

    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.ClassMethod = SomeClassMethod
    this.ClassMethod2 = SomeClassMethod2
    }

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

    function SomeClassMethod2()
    {
    alert("method2")
    }

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

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

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

    function DoAsyncRequest(func, callbackFunction)
    {
    timerCounter++;
    return window.setTimeout( function(){CallFunc(func, callbackFunction)}, 500);
    }

    //timerId = DoAsyncRequest(this.method, this.method2);
    var someClass = new SomeClass;
    timerId = DoAsyncRequest(someClass.ClassMethod, someClass.ClassMethod2);
    timerId = DoAsyncRequest(someClass.ClassMethod, someClass.ClassMethod2);
    timerId = DoAsyncRequest(someClass.ClassMethod, someClass.ClassMethod2);
    timerId = DoAsyncRequest(someClass.ClassMethod, someClass.ClassMethod2);
    timerId = DoAsyncRequest(someClass.ClassMethod, someClass.ClassMethod2);
    timerId = DoAsyncRequest(someClass.ClassMethod, someClass.ClassMethod2);


    </script>
     
    , Feb 16, 2004
    #3
  4. [This followup was posted to comp.lang.javascript and a copy was sent to
    the cited author.]

    In article <>,
    says...

    > 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


    [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
     
    Michael Winter, Feb 16, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jure Sah
    Replies:
    21
    Views:
    968
  2. =?Utf-8?B?QWRuYW4=?=

    Asynchronus call in ASP.NET page?

    =?Utf-8?B?QWRuYW4=?=, Jul 24, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    523
    =?Utf-8?B?QWRuYW4=?=
    Jul 24, 2007
  3. Replies:
    4
    Views:
    350
    James Kanze
    Jul 11, 2008
  4. Phalanx
    Replies:
    0
    Views:
    356
    Phalanx
    Dec 10, 2008
  5. vicky_smiles007

    Asynchronus binding grid view

    vicky_smiles007, Jul 17, 2011, in forum: .NET
    Replies:
    0
    Views:
    962
    vicky_smiles007
    Jul 17, 2011
Loading...

Share This Page