Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Asynchronus calls and callbacks to Object orientated code

Reply
Thread Tools

Asynchronus calls and callbacks to Object orientated code

 
 
foldface@yahoo.co.uk
Guest
Posts: n/a
 
      02-12-2004
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
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      02-12-2004
On 12 Feb 2004 07:22:12 -0800, <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)d (replace ".invalid" with ".uk" to reply)
 
Reply With Quote
 
 
 
 
foldface@yahoo.co.uk
Guest
Posts: n/a
 
      02-16-2004
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>
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      02-16-2004
[This followup was posted to comp.lang.javascript and a copy was sent to
the cited author.]

In article <(E-Mail Removed) >,
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Asynchronus binding grid view vicky_smiles007 ASP .Net 0 07-17-2011 02:33 AM
Stuck with Java Objected Orientated Programming - Array Phalanx Java 0 12-10-2008 01:58 PM
need help in object orientated programming alck1234@gmail.com C++ 4 07-11-2008 09:20 AM
Asynchronus call in ASP.NET page? =?Utf-8?B?QWRuYW4=?= ASP .Net 2 07-24-2007 04:02 PM
XML-based object orientated pre-Assembler Jure Sah XML 21 03-14-2005 05:18 AM



Advertisments