Asynchronus calls and callbacks to Object orientated code

F

foldface

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
 
M

Michael Winter

[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
 
F

foldface

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>
 
M

Michael Winter

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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top