Jim,

What I've always done to counter this problem is to create an API
class--which I use to monitor instances of each class and hold "global"
functionality. For example:

function _HRPApi(){
        this.instance = [];
}

_HRPApi.prototype.register = function (obj){
        var iInstance = this.instance.length;
        this.instance[iInstance] = obj;
        return iInstance;
}

// create the global class
HTTPRequestPoolApi = new _HRPApi();

Now in your actual class, you can do something like:

function HTTPRequestPool(){
        this.instance = HTTPRequestPoolApi.register(this);
        this.interval = 500; // number of ms to wait before setTimeout
}

Now, anytime you need to reference the current instance of the class from a
global function (such as inside the setTimeout) you've got a copy stored in
a global variable (HTTPRequestPoolApi) you can access it with something like
the following code:

setTimeout(
        "HTTPRequestPoolApi.instance[" + this.instance + "].method();", 
        this.interval
);

Hope this helps!

-Dan

>-----Original Message-----
>From: Jim Davis [mailto:[EMAIL PROTECTED]
>Sent: Tuesday, March 21, 2006 10:37 AM
>To: CF-Talk
>Subject: OT: JavaScript Scoping Gurus?
>
>I'm building an HTTP request pooler in JS.  In essence the pool creates
>several HTTP objects and allows the programmer to add requests to the
>queue.
>The queue is then examined at an interval and queued requests are passed to
>free objects for calls.
>
>Anyway - all that seems to work pretty well - except setting up the
>interval.
>
>Let's assume this simplified Object:
>
>       // Create the Pool
>function HTTPRequestPool(ObInstanceCount, IntervalCount) {
>
>               // Create a Queue for Requests
>       this.RequestQueue = new Array();
>
>       App.RequestPoolTimer = window.setInterval("this.doRequest();", 500);
>
>};
>
>HTTPRequestPool.prototype.addRequest = function addRequest( Method, URL,
>Parameters, Handler) { };
>
>HTTPRequestPool.prototype.doRequest = function doRequest() { };
>
>
>We would instantiate this like this (someplace on the page):
>
>RequestPool = new HTTPRequestPool(5, 500);
>
>The problem is the interval - in this case the doRequest() method, when
>called from the interval, loses the connection to the parent object.  When
>called "this" refers to the top-level window (as it would when a top-level
>function is called) NOT to the parent object (as it would when a method is
>called).
>
>However if I remove the interval from the object and instantiate things
>like
>this (setting the interval outside of the object):
>
>RequestPool = new HTTPRequestPool(5, 500);
>App.RequestPoolTimer = window.setInterval("RequestPool.doRequest();", 500);
>
>Everything works fine - the interval never loses its reference.
>
>This is a workaround, but I'd much rather have the interval contained in
>the
>object itself.
>
>Thanks in advance for any ideas.
>
>Jim Davis
>
>
>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:235865
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to