By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,702 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,702 IT Pros & Developers. It's quick & easy.

Asynchronus calls and callbacks to Object orientated code

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 12 Feb 2004 07:22:12 -0800, <fo******@yahoo.co.uk> 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
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2

P: n/a
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>
Jul 20 '05 #3

P: n/a
[This followup was posted to comp.lang.javascript and a copy was sent to
the cited author.]

In article <ac**************************@posting.google.com >,
fo******@yahoo.co.uk 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
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.