B
bizt
Hi,
I have an AJAX class that is designed to be instantiated many times
where ever it may be used. For example, if I have a function, when I
call that function an instance of the AJAX class is created (and
hopefully destroyed at the end). See:
function makePricesCall() {
var ajax = new AjaxClass;
ajax.url = "getPrices.php";
ajax.onSuccess = function {...};
ajax.send();
}
The function instantiates the class, sets the url and handler function
(onSuccess) then makes the call (send). Now, what I do like about this
is that I can make simultaneous calls from different instances of the
same class. For example, in my app I have two calls which combined
build a single page. The first call is to get the market info and is
typically called once. The second call is to get the prices of that
market (which change frequently) and this is called every 20 seconds
to keep these up to date. The first time the page is rendered it makes
a call to both at the same time, the function that builds the page
waits til both data feeds have been received. This is faster than
calling one then calling the other and to my knowledge the only way I
can do this is by having two instances of my class (which internally
have their own unique instance of XHR). Even on each refresh of the
prices though a new class instance is created
What I am worried about is that over time because Im always
instantiating new objects that these will take up memory, even the old
objects no longer in use. Should I perhaps have a unique instance of
the AJAX class for each data feed, this way I can make simultaneous
calls to two different feeds (market and price) but when I need to
make a second call to the prices it uses the same AJAX object as
before and doesn't have to create a new one. This way I could
incorporate some queuing also. Ive always gone on the assumption
however that when a variable (in this case an object of my class) is
created locally to that function it is destroyed when the function
ends but when the function ends it must be kept somewhere as its still
able to call the handler function onSuccess. Is this the case? After a
few hours of the app running are these just going to be taking up more
memory than is really needed?
Anyway would be great to get some feedback on recommend theory when
building such a class, be much appreciated. Im aware there are lots of
ready made AJAX wrapper classes out there but I prefer to build my own
so I can add my own things to them when they are needed (eg. ajax
caching)
Cheers
Burnsy
I have an AJAX class that is designed to be instantiated many times
where ever it may be used. For example, if I have a function, when I
call that function an instance of the AJAX class is created (and
hopefully destroyed at the end). See:
function makePricesCall() {
var ajax = new AjaxClass;
ajax.url = "getPrices.php";
ajax.onSuccess = function {...};
ajax.send();
}
The function instantiates the class, sets the url and handler function
(onSuccess) then makes the call (send). Now, what I do like about this
is that I can make simultaneous calls from different instances of the
same class. For example, in my app I have two calls which combined
build a single page. The first call is to get the market info and is
typically called once. The second call is to get the prices of that
market (which change frequently) and this is called every 20 seconds
to keep these up to date. The first time the page is rendered it makes
a call to both at the same time, the function that builds the page
waits til both data feeds have been received. This is faster than
calling one then calling the other and to my knowledge the only way I
can do this is by having two instances of my class (which internally
have their own unique instance of XHR). Even on each refresh of the
prices though a new class instance is created
What I am worried about is that over time because Im always
instantiating new objects that these will take up memory, even the old
objects no longer in use. Should I perhaps have a unique instance of
the AJAX class for each data feed, this way I can make simultaneous
calls to two different feeds (market and price) but when I need to
make a second call to the prices it uses the same AJAX object as
before and doesn't have to create a new one. This way I could
incorporate some queuing also. Ive always gone on the assumption
however that when a variable (in this case an object of my class) is
created locally to that function it is destroyed when the function
ends but when the function ends it must be kept somewhere as its still
able to call the handler function onSuccess. Is this the case? After a
few hours of the app running are these just going to be taking up more
memory than is really needed?
Anyway would be great to get some feedback on recommend theory when
building such a class, be much appreciated. Im aware there are lots of
ready made AJAX wrapper classes out there but I prefer to build my own
so I can add my own things to them when they are needed (eg. ajax
caching)
Cheers
Burnsy