Forum Moderators: open

Message Too Old, No Replies

Recursive window.setTimeout()

         

vol7ron

4:01 pm on Mar 3, 2008 (gmt 0)

10+ Year Member



We're all familiar with setTimeout() not being able to pass objects, except as strings, but window.setTimeout() is supposed to be able to. The following is so close, but the properties of the class are still being dropped.

For ease of development, pageLoad is called in <body onload="javascript:pageLoad();">. It creates the instances. The changeImage is called on rollout. Obviously, the first call is fine, but as with other people whom have setTimeout problems, the properties of the object are dropped, even though no errors are given.

[pre]function pageLoad() {
link1 = new linkClass('home_link1',null,0,false);
link2 = new linkClass('home_link2',null,0,false);
//link1.msg();
}[/pre]

[pre]function linkClass(objId, obj, pos, stop) {
this.obj = obj!=null?obj:getObjById(objId);
this.objId = objId;
this.pos = pos;
this.stop = stop;
this.changeImage = changeImage;
this.highlight = highlight;
this.msg = function(){alert(
"this: " + this + "\n" +
"objId: " + this.objId + "\n" +
"obj: " + this.obj + "\n" +
"pos: " + this.pos + "\n" +
"stop: " + this.stop + "\n");}
}[/pre]

[pre] function changeImage() {
with (this) {
//msg();
if (typeof obj == 'undefined') getObjById(objId);
if (typeof pos == 'undefined') pos = -72;
if (!stop) {
if (pos > -288) {
obj.style.background = "#000 url(Menu_Home.png) no-repeat " + pos + "px 0;";
pos -= 72;
window.setTimeout(this.changeImage,275);
}
} else {
obj.style.background = "#000 url(Menu_Home.png) no-repeat 0px 0;";
}
}
}[/pre]

What I'm trying to do is use CSS image rollovers. Only, instead of a two-state effect (mouse on/off image), there will be a mouse-on and 3 or 4 images will show in sequence when mouse is off. I had this working when not using the javaScript class, but I wanted to make my code a little more robust.

Thank you,
vol7ron





[edited by: vol7ron at 4:38 pm (utc) on Mar. 3, 2008]

vol7ron

4:14 pm on Mar 3, 2008 (gmt 0)

10+ Year Member



The answer, encapsulate the call in a function.
[pre]window.setTimeout(function (){changeImage()},275);[/pre]



Any ideas on how to improve performance of the above code? Without the obvious minification techniques?

I assume making changeImage() and highlight() prototypes would be best on memory - I think they would still have the same functionality. I also think using clearTimeout in combination with the "stop" boolean might help free resources faster.

I'm curious if there was a more efficient way to delay something other than using setTimeout/setInterval.

Thank you,
vol7ron