jugheadyong
asked on
Javascript Recursive Object Method
Dear Experts,
My object "RotatingBanner" has one function "RotateBanner()".
These are declared below:
************************** ********** ******
function RotateBanner()
{
if (!document.images) return;
if (this.nowIsFirstPass == 1)
{
this.nowIsFirstPass = 0;
}
else
{
if (this.nowRotationMode == 0)
{
while (this.current == this.old)
{
this.current = Math.floor(Math.random()*t his.banner s.length);
}
}
if (this.nowRotationMode == 1)
{
this.current = this.current + 1;
if (this.current >= this.banners.length)
{
this.current = 0;
}
}
}
this.old = this.current;
document.images[this.image Name].src = this.banners[this.current] ;
for (nowNumber = 0; nowNumber < document.links.length; nowNumber++)
{
if(document.links[nowNumbe r].name == this.anchorName)
{
document.links[nowNumber]. href = this.bannerLinks[this.curr ent];
}
}
setTimeout('this.Rotate()' ,this.rota teFrequenc y);
}
function RotatingBanner(inputImageN ame, inputAnchorName, inputBannerArray, inputBannerLinkArray, inputRotationMode, inputTimeout)
{
this.old = 0;
this.current = 0;
nowRotationMode = inputRotationMode; // 0 = random, 1 = sequential
this.nowIsFirstPass = 1;
this.banners = inputBannerArray;
this.bannerLinks = inputBannerLinkArray;
this.imageName = inputImageName;
this.anchorName = inputAnchorName;
this.rotateFrequency = inputTimeout;
this.Rotate = RotateBanner;
}
************************** ********** ********** **********
The object is instantiated by this code:
************************** ********** ********** **********
<a name="endorsementlink" href="test.html" target="_blank"><img src="" name="endorsement" border=0></a>
<script language="javascript">
var endorsements = new Array(
'/commons/endorsements_ACC APremierPl us.gif',
'/commons/logo_Singapore_Q uality.gif '
);
var endorsementLinks = new Array(
'http://www.accaglobal.com/colleges/registrationscheme/benefits',
'http://www.spring.gov.sg/'
);
var nowEndorsements = new RotatingBanner('endorsemen t', 'endorsementlink', endorsements, endorsementLinks, 1, 3000);
nowEndorsements.Rotate();
</script>
************************** ********** ********** **********
The problem I have is with the timeout statement in the object's method:
setTimeout('this.Rotate()' ,this.rota teFrequenc y);
If I do :
setTimeout('this.RotateBan ner()',thi s.rotateFr equency);
The method will be called, but not with the same instance of the object (thus, all variables are undefined).
If I do:
setTimeout('this.Rotate()' ,this.rota teFrequenc y);
The browser complains that the function is undefined.
I need the instanced object to continue to call its own internal method.
How then, should this be done?
Yours Sincerely,
Jonah.
My object "RotatingBanner" has one function "RotateBanner()".
These are declared below:
**************************
function RotateBanner()
{
if (!document.images) return;
if (this.nowIsFirstPass == 1)
{
this.nowIsFirstPass = 0;
}
else
{
if (this.nowRotationMode == 0)
{
while (this.current == this.old)
{
this.current = Math.floor(Math.random()*t
}
}
if (this.nowRotationMode == 1)
{
this.current = this.current + 1;
if (this.current >= this.banners.length)
{
this.current = 0;
}
}
}
this.old = this.current;
document.images[this.image
for (nowNumber = 0; nowNumber < document.links.length; nowNumber++)
{
if(document.links[nowNumbe
{
document.links[nowNumber].
}
}
setTimeout('this.Rotate()'
}
function RotatingBanner(inputImageN
{
this.old = 0;
this.current = 0;
nowRotationMode = inputRotationMode; // 0 = random, 1 = sequential
this.nowIsFirstPass = 1;
this.banners = inputBannerArray;
this.bannerLinks = inputBannerLinkArray;
this.imageName = inputImageName;
this.anchorName = inputAnchorName;
this.rotateFrequency = inputTimeout;
this.Rotate = RotateBanner;
}
**************************
The object is instantiated by this code:
**************************
<a name="endorsementlink" href="test.html" target="_blank"><img src="" name="endorsement" border=0></a>
<script language="javascript">
var endorsements = new Array(
'/commons/endorsements_ACC
'/commons/logo_Singapore_Q
);
var endorsementLinks = new Array(
'http://www.accaglobal.com/colleges/registrationscheme/benefits',
'http://www.spring.gov.sg/'
);
var nowEndorsements = new RotatingBanner('endorsemen
nowEndorsements.Rotate();
</script>
**************************
The problem I have is with the timeout statement in the object's method:
setTimeout('this.Rotate()'
If I do :
setTimeout('this.RotateBan
The method will be called, but not with the same instance of the object (thus, all variables are undefined).
If I do:
setTimeout('this.Rotate()'
The browser complains that the function is undefined.
I need the instanced object to continue to call its own internal method.
How then, should this be done?
Yours Sincerely,
Jonah.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I believe Basilisci is right,. Here is an excellent article on closures..
http://developer.mozilla.org/en/docs/A_re-introduction_to_JavaScript#Closures
http://developer.mozilla.org/en/docs/A_re-introduction_to_JavaScript#Closures
ASKER
Outstanding, Basilisci !
And thanks for the link to the closures, vbandaru. I have learnt something new about Javascript today.
And thanks for the link to the closures, vbandaru. I have learnt something new about Javascript today.
You should use a global variable.
Anyway that's strange 'cause the garbage collector should not destroy things if the ref counter is > 0 (like for "this" in this case).