473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

creating multiple response handler functions with a loop

I'm creating a page with multiple "widgets", each with its own
XMLHttpRequest object, so the user can play with the widgets in
parallel. The widgets are enclosed in divs with class "widget" and a
unique ID which tells me the name of the PHP file for the widget.
Each widget should be able to access its PHP file with GET and POST
methods, and to re-draw its innerHTML on response.

I am using a simple Ajax library called XHConn (http://xkr.us/code/
javascript/XHConn/) by Brad Fults. Making the requests from within
each widget doesn't seem too hard, but the response handling is a
little difficult. Each response handler function should write output
to the correct widget. But if I create all these response handler
functions with a loop, they all use the latest "i" on execution. See:

widgetArray = getElementsByClassName("widget");
for (var i=0; i<widgetArray.length; i++) {
widgetConn[widgetArray[i].id] = new XHConn();
widgetReloaders[widgetArray[i].id] = function (oXML)
{ widgetArray[i].innerHTML=oXML.responseText };

As you see I dynamically create three arrays, one for the divs, one
for the XMLHttpRequest objects, and one for the response handlers.
The problem is the "widgetArray[i]" in the response handler. I want
it to use the "i" at the time I created the function, not the "i" at
the time of execution (which is always one past the end of the
array). Can I do this? Is there a better way?

Oct 9 '07 #1
1 2463
On Oct 9, 8:21 am, joe_doufu wrote:
<snip>
... . But if I create all these response handler
functions with a loop, they all use the latest "i" on execution.
See:

widgetArray = getElementsByClassName("widget");
for (var i=0; i<widgetArray.length; i++) {
widgetConn[widgetArray[i].id] = new XHConn();
widgetReloaders[widgetArray[i].id] = function (oXML)
{ widgetArray[i].innerHTML=oXML.responseText };
<snip>

So the problem is with your understanding of closures in javascript.
See:-

<URL: http://jibbering.com/faq/faq_notes/closures.html >

A closure in javascript is an association of a function object and the
objects on the scope chain at the point where the function object is
created (note that some web resources on javascript closures
erroneously suggest that the closure is formed between the function
object and the local variables). The objects on the scope chain are
mostly the Variable objects which have named properties that hold the
values of local variables. When you change a local variable, such as
your - i - you are changing the value of a property of a Variable
object, and as all your function objects form their individual
closures with the same variable object they all see the same changed
value.

Oct 9 '07 #2

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

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
8
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE,...
0
by: Chris Hall | last post by:
The records in my database are displayed in a form as follows: %> <form action="report-ammend1.42.asp" method="post"name="form"> <table border=1> <% x = 1
11
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a...
9
by: Daniel Moree | last post by:
I'm using MS VC++ 6.0 I'm working on a big project. I've currently have several files for this project. Here's the problem. I have one header file phead.h I have two code files main.cpp and...
3
by: deko | last post by:
I have a logging routine that's supposed to silently log errors caught by error handler code on certain functions. The problem is sometimes stuff happens and the error handler can get caught in a...
12
by: cj | last post by:
I would like to have menu items a main menu bar that represent the days of the week. When you click on them they alternate from checked to unchecked. Right now I have 7 subs that look like this...
1
by: dnes | last post by:
Open Multiple New Browser Windows from ASP.NET I'm having trouble figuring out how to open multiple new browser windows (each one displaying something different). As you can see from the code...
10
by: Daniel Loose | last post by:
hi dear folks, i'm new to ajax. simple request no problem - but it seems when I set the response handler function, I can only pass the function name and not give parameters, so I have a problem...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.