Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Object refference is lost in a loop? My first JS attempt, please help

Reply
Thread Tools

Object refference is lost in a loop? My first JS attempt, please help

 
 
Lae.
Guest
Posts: n/a
 
      12-07-2005
I can't figure this one out. n00b question no doubt, this is my first
ever JS attempt.

Here's the snippet, and here's the full deal
http://www.ualberta.ca/~koryb/first.js
http://www.ualberta.ca/~koryb/
http://www.ualberta.ca/~koryb/test.html runs it

// Runs timer loop and increments pics
function timer()
{ this.incrementSrc()
this.timerControl = setTimeout("timer()", this.counterTime)
}

// Increments the pic number, updates pic src and diplays update
function incrementSrc()
{ var aNumber = this.picNumber
aNumber++
alert(aNumber)
if (aNumber > numberOfPics)
{ aNumber = 1
}
this.newPicNumber(aNumber)
this.newSrcName(sourceGenerator(this.picNumber))
document.getElementById(this.id).src = this.srcName
}

This runs the first loop ok, but the 2nd loop I get a no properties
error for
document.getElementById(this.id).src = this.srcName Actully the whole
object isn't being reffered to anymore it seems. Why?

I can't figure this out.

Thanks for your time,
Lae.

 
Reply With Quote
 
 
 
 
Dennis Ålund
Guest
Posts: n/a
 
      12-07-2005
When you first call slotOne.timer() it's OK because then it's a call to
the member function timer of the object slotOne. But within
slot::timer() you have this:
function timer()
{ this.incrementSrc()
this.timerControl = setTimeout("timer()", this.counterTime)
}
Where you only call timer() without as if it wasn't a member function
of the class. And since you have declared timer() as a stand alone
function (and afterwards assigned it to the class) it is a perfectly
valid function call... although "this" has no meaning when the window
timer calls the function (since the function that was called is not
member of an object).

Did I make any sense?

 
Reply With Quote
 
 
 
 
Lae.
Guest
Posts: n/a
 
      12-07-2005
Sorta. I am new at this.

So I have to figure out a way to reffer back to the object and then to
the timer again.
So by I expand the loop with another fuction that maintains the
refference it should work then eh?

I'll fiddle with it and get back to ya if I need more help. lol A
night's sleep always works too. (o:

Thanks,
Lae.

 
Reply With Quote
 
Dennis Ålund
Guest
Posts: n/a
 
      12-07-2005
Lae. wrote:
> So I have to figure out a way to reffer back to the object and then to
> the timer again.

Yes, you have to make a reference to the calling object in
window.setTimer()

> So by I expand the loop with another fuction that maintains the
> refference it should work then eh?

Yea, or maybe you can attach some object to a image DOM element:

document.getElementById("imageNo1").srcChanger = new
SourceChanger("imageNo1");
function SourceChanger(id)
{
this.imageId = id;
this.timeoutInterval = 1234;
}
SourceChanger.prototype =
{
changeSource: function()
{
...
},
startTicker: function()
{
changeSource();

window.setTimeout("document.getElementById(\""+thi s.imageId+"\").startTicker()",
this.timeoutInterval);
}
}


This is just something I came up with in the moment of writing... in
other words it's just one of many possible ways of solving it... this
was the first that came to my mind.

 
Reply With Quote
 
Dennis Ålund
Guest
Posts: n/a
 
      12-07-2005
Edit note:
There should be a call to startTicker in the constructor of
SourceChanger... didn't pay too much attention of what I was doing
there...

It aint a complete solution, but you get the idea. It's a push in some
direction if you're stuck.

 
Reply With Quote
 
Dennis Ålund
Guest
Posts: n/a
 
      12-07-2005
Edit note 2:
How embarrasing... another err (this is what you get for being hasty)
This
window.setTimeout("document.getElementById(\""+thi s.imageId+"\").startTicker()",
this.timeoutInterval);

should of course look like this
window.setTimeout("document.getElementById(\""+thi s.imageId+"\").srcChanger.startTicker()",
this.timeoutInterval);

But you already figured that out
Let's end this monologue with this post.

 
Reply With Quote
 
Lae.
Guest
Posts: n/a
 
      12-10-2005
Thanks for all the help Dennis. Lucky for me I didn't look at it
till just now.

I did this:
function timer(object)
{ this.object = object
this.object.incrementSrc()
this.object.timerControl = setTimeout("timer(this.object)",
this.object.counterTime)
}

Just passes an object into it and kept handing it back. To be honest a
lot of what you wrote above is a bit beyond me at the moment. I can
understand passing messages simply and I have a small repetiore of
stuff to work with. I will try your suggestion here and see how it
goes.

I am currently stuck with the problem of wanting to run 3 of these
timed loops at once. Is that possible? How can I get three of them
working independently? Or would I have to create them and find the
differents and then run a larger loop from an array or something like
that? That seems like it would be hard since I wanted the times to be
instantiated with a randomness.

Ideas?
Maybe I should repost this under a new topic? I dunno.

thanks though,

Kory

 
Reply With Quote
 
Dennis Ålund
Guest
Posts: n/a
 
      12-11-2005
It's no problem having several timers going parallell.
Although, it might give you some strange results if you pass
"this.object" in the timer. You see the timer will interpret the string
when "counterTime" is due... in other words, "this" will refer to
"window" at that time.
What you might want to do is to pass an string id that can be used to
find the target object... for example an HTML element id (to which you
have attached the object). This is going back a little to the solution
I proposed... perhaps you're working on another trail, but the idea of
passing a string id is the same.

The timer function is probably best implemented as a static function or
something, since you can't make out the instance name of the object
that hold you timer function. This will require that you pass all
necessary information as parameters...
You can make a static function in JavaScript by declaring it like this
(apply this to your timerControl function):
function MyClass() = { ... }
MyClass.myStaticFunction = function () { ... }


So, to activate several timers going parallell you just call your timer
function repeatedly with different object id.

timer(myObjectId, interval)
{
doStuff();
setTimeout("timer("+myObjectId+", "+interval+")", interval);
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
server transfer and previous page refference issue Yankee Imperialist Dog! ASP .Net 2 04-25-2009 07:36 PM
GOOD REFFERENCE BOOK NEEDED ec VHDL 3 11-21-2006 08:23 AM
Refference my current frame Tim Javascript 0 03-14-2005 07:44 AM
problem with tk and pass by refference (I think :) Matthew Thorley Python 4 02-11-2005 08:27 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments