473,769 Members | 6,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.incrementS rc()
this.timerContr ol = setTimeout("tim er()", this.counterTim e)
}

// 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.newPicNumb er(aNumber)
this.newSrcName (sourceGenerato r(this.picNumbe r))
document.getEle mentById(this.i d).src = this.srcName
}

This runs the first loop ok, but the 2nd loop I get a no properties
error for
document.getEle mentById(this.i d).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.

Dec 7 '05 #1
7 1557
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.incrementS rc()
this.timerContr ol = setTimeout("tim er()", this.counterTim e)
}
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?

Dec 7 '05 #2
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.

Dec 7 '05 #3
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.getEle mentById("image No1").srcChange r = new
SourceChanger(" imageNo1");
function SourceChanger(i d)
{
this.imageId = id;
this.timeoutInt erval = 1234;
}
SourceChanger.p rototype =
{
changeSource: function()
{
...
},
startTicker: function()
{
changeSource();

window.setTimeo ut("document.ge tElementById(\" "+this.imageId+ "\").startTicke r()",
this.timeoutInt erval);
}
}
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.

Dec 7 '05 #4
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.

Dec 7 '05 #5
Edit note 2:
How embarrasing... another err (this is what you get for being hasty)
This
window.setTimeo ut("document.ge tElementById(\" "+this.imageId+ "\").startTicke r()",
this.timeoutInt erval);

should of course look like this
window.setTimeo ut("document.ge tElementById(\" "+this.imageId+ "\").srcChanger .startTicker()" ,
this.timeoutInt erval);

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

Dec 7 '05 #6
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.inc rementSrc()
this.object.tim erControl = setTimeout("tim er(this.object) ",
this.object.cou nterTime)
}

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

Dec 10 '05 #7
It's no problem having several timers going parallell.
Although, it might give you some strange results if you pass
"this.objec t" in the timer. You see the timer will interpret the string
when "counterTim e" 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.myStati cFunction = function () { ... }
So, to activate several timers going parallell you just call your timer
function repeatedly with different object id.

timer(myObjectI d, interval)
{
doStuff();
setTimeout("tim er("+myObjectId +", "+interval+ ")", interval);
}

Dec 11 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3092
by: Tom | last post by:
I want to open a recordset object on an .asp page. When I open the recordset I would like to use a stored procedure that expects a parameter to be passed for the stored procedure. I will then use the recordset to loop thru the recordset, update values from the recordset and then update the database by passing parmeters to another stored procedure. I would like to use the recordset object but can it be used to pass a parameter to a stored...
7
1494
by: Francis Bell | last post by:
Hello, I've got a 25 line file with lines of data like this: sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1 The first field is the code that determines what to do. I need to loop through this data file and, based on that first field, execute different cases in a switch statement (yes, there's only one now...I'm in a building process, and I need to get the first one to read first.). However, it's only going through 1 time and it should...
5
2517
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj);
7
1986
by: multicherry | last post by:
Hi, Having searched for a way to fetch a window object by name, all I came across were answers along the line of... "All you have to do is say windowObj = window.open("blah", "name");" which isn't very useful if you want to fetch information from an existing document in the window. One solution I came up with was quite simple; open the "new" window, giving you a reference to the object, then use window.history.back() to
14
2817
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that shows all the properties, weeks available, pricing, etc. for a particular area of Europe. The data varies widely depending on the region; at times there will be 50 properties, and at other times only a half dozen. The cross referencing of...
32
2570
by: Joe | last post by:
I am just starting to use Object Oriented PHP coding, and I am seeing quite often the following (this example taken from a wiki): $wakka =& new Wakka($wakkaConfig); What exactly is the =&, and why is it different from = ?
3
1147
mrmiffo
by: mrmiffo | last post by:
Hi all! I have probably a simple problem but, as for all problems it is huge for me since I'm the one having it... I'm trying to work my way up to the mother of mothers in an XML file starting with a clicked node and what goes wrong is that even when I get the parents' label - the parent is not acknowledged as an object... This I have in a loop: myLabel = currentNode.parentNode.getAttribute('label'); currentNode =...
9
2991
by: Peter Webb | last post by:
I want to animate one object moving in front of another. I cannot re-render the background as the object moves, as it would be extremely time consuming. This is what I would like to do. I draw the background to a Graphics object, copy it, draw the front object on the original, render it to the screen, then reload the copy (Clone?) of the background for the next loop. I couldn't find any way of doing this directly, and if there is I...
5
2003
by: adinda | last post by:
So what i need is this; (I'm very new at this,, programming in C I mean...) In matlab I had a while loop, and after each loop was done I added my resulting matrix to an object. Seeing the loop is conditional to my results I do not know in advance how many loops I need. The matrices are Nx * Ny* Nz*6. In C I put my matrices into 1D arrays. So I thought that if I use a counter for each time i do a loop and the calloc and realloc function
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.