Link to home
Start Free TrialLog in
Avatar of BartS
BartS

asked on

setTimeout in objects causes out of stack stack space

I'm trying to add setTimeout in a function as a method to all objects created dynamically from DIVs in the document.
This is to be able to set the setTimeout to each DIV separateley. But function Timer is executed repeatedly without waiting 1000mseconds as specified by setTimeout and causes Out of stack stack space message. The functions look like:

function myobjs(obj) {
this.objTimer = Timer;

//other methods here
}

function Timer() {
this.objSetLeft(-20);
setTimeout(objs['Layer6'].oTimer(), 1000);//Out of stack stack space
}

Any ides how to fix this?
Avatar of a.marsh
a.marsh

I'm not fully clear on what you are actually trying to do....but I suspect we need to see all of your code.

Just in case, have you tried:

setTimeout("objs['Layer6'].oTimer()", 1000);


Ant
The quotes will fix it. This way a new timer will be created right away which will result in out of stack spaace (thousands of timers...whoopie) :-)

Regards,
CJ
Avatar of BartS

ASKER

Ant,

Your line fixes the stack error.

function Timer() {
this.objSetLeft(-20);
setTimeout("objs['Layer6'].oTimer()", 1000);
}
So, the above functions well. But I'm limited to hard coding Layer6.
I need to send different objects ( DIV id's) to Timer(). Therefore, the line:
setTimeout("objs['Layer6'].oTimer()", 1000);
needs to be changed to something like:
setTimeout("objs['this'].oTimer()", 1000);
so that it takes the self object as an argument.
This last line causes: Error 'obj.this' is null or not a not an object.

I also tried:
setTimeout("objs['"+this+"'].oTimer()", 1000);
setTimeout("objs["+this+"].oTimer()", 1000);
but no success.

Thanks you both
Bart
ASKER CERTIFIED SOLUTION
Avatar of a.marsh
a.marsh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BartS

ASKER

Hi,

setTimeout is still not working, but it seems to be very close to a solution.
Here is the whole code:

<HTML><HEAD>
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">



<SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript">
<!--
function aobjs(obj) {
     this.css2 = obj;
     this.oSetLeft = SetLeft;
     this.oGetLeft = GetLeft;
     this.oTimer = Timer;
}

function SetLeft(left) {
     this.css2.style.left = left + "px";
}
function GetLeft() {
    var lt = parseInt(this.css2.style.left);
     return lt;
}

function makeObj() {
     var allTags = document.all.tags("DIV");
     objs = new Array();
     objs[allTags[0].id] = new aobjs(allTags[0])
     objs[allTags[1].id] = new aobjs(allTags[1]);
}

function Timer(obj) {
//alert( this.id )
this.oSetLeft(this.oGetLeft()+ 60);//merge
setTimeout("objs['" + this.id + "'].oTimer()", 1000);

//objs['Layer6'].oTimer()
//setTimeout(objs['Layer6'].oTimer(), 1000);//Out of stack stack space
//setTimeout("objs["+this+"].oTimer()", 1000);
}
//-->
</SCRIPT>
</HEAD>


<BODY BGCOLOR="#FFFFFF" style="margin:0px" onload="makeObj()">
<a href="#" onmouseover="objs['Layer1'].oTimer();">TEST</a>

  <div id="Layer1" style="position:absolute; z-index:2; left: 50px; top: 208px"><a HREF="javascript:OpenWin(1);"><img src="thumb/Dsc00178.jpg" name="Img1" width="100" height="72" border="0"></a></div>

  <div id="Layer2" style="position:absolute; z-index:4; left: 70px; top: 108px"><a HREF="javascript:OpenWin(2);"><img src="thumb/Dsc00179.jpg" name="Img2" width="100" height="72" BORDER=0></a></div>

</BODY></HTML>
I can't really have a good look at this until I get home - but what is an "aobjs"? I've never heard of such a thing.......

What exactly is the error you are now getting?

Ant
sounds like my coding style

a -> active
obj -> object
s -> style

It kind of means the current active object's style. :-)

and in his case it is an object he has created.
Whoops!

I didn't see the function for aobjs......!

:oP

Ant
I don't know what you are trying to achieve, but try:

    function aobjs(obj)
    {
       this.css2 = obj;
       this.id = obj.id
       this.oSetLeft = SetLeft;
       this.oGetLeft = GetLeft;
       this.oTimer = Timer;
    }

Regards,
CJ
The only thing I can think of simply by looking at the code is that the objs array is not in scope - tell us what the error is you're getting and we can go from there.

Ant
CJ is correct you need to add the line:

this.id = obj.id;


Seeing as we both helped you in this question I recommend that you split the points between us.

To do that you will need to post a 0 point question in Community Support to get a moderator reduce the points on this question for you.

Once they have done that then you will be able to accept an answer.

:o)

Ant
Avatar of BartS

ASKER

IT's a miracle!
It works. And it makes sense.

How am I going to split the points between you now?
Thanks a lot.

Bart

N.B.
C.J. What I'm trying to achieve is to set the setTimeout to each DIV separateley. So that I can animate any DIV separately, independently. Is there a better way?

Actually, now I noticed there is a disadvantage of the above code:
If the object receives more then one mouseover, this will creat more than one instance of setTimeout of the same object. And this may not be desired.

The name "aobjs" does not ahve much meaning I created a few versions of this function for testing, so I had to make each of it distinct.

Ant, you say there is no need for this function. Is there a way to add methods to the objects without it?


Avatar of BartS

ASKER

I can give you each 300 if I knew how.
Well if you're happy to give that many points away then the way to do that is to accept one of our comments as an answer on this question and then post a new question in the Javascript area with a subject of (for example):

Points for a.marsh

and in the question body, mention that the points are for help on this question.

However don't feel that you have to give away all those points - you can ask community support to reduce the points on this question to 150 and then post a new question for 150 points as above.

It's up to you - what would you prefer to do?

:o)

Ant
Hi BartS,

I've reduced the points from 300 to 150.

You can now award this question to one of the two experts, and, as a.marsh says in the above comment, post another dummy question for 150 points titled "Points for <the other expert>" in which you mention "for your help with https://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20150939"

Cheers

modder
Community Support Moderator@Experts-Exchange
Thanks modder.

:o)

Ant