473,782 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = getElementsByCl assName("widget ");
for (var i=0; i<widgetArray.l ength; 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 "widgetArra y[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 2489
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 = getElementsByCl assName("widget ");
for (var i=0; i<widgetArray.l ength; 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
13225
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: Hunter_69. Here is what the form looks like. I have the difficulty of inserting the multiple items selected by the user the first time he visits and uses the screen and then using an UPDATE when he visits later. Model | Original Price | Reduced Price...
8
5521
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, UPDATE, DELETE) which are controlled by a web frontend and the table records are manipulated to control the permissions. Example: The Press Release section record would look like this: Username: John Doe Function Name: Press Release
0
2408
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
2183
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 datagrid. I am using the viewstate object to store the Arraylist items on the page on postback. My PROBLEM is that I need to redirect the user to a new aspx page and on this new page i need to be able to access the items in my arraylist. Is this...
9
2593
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 gameloop.cpp phead.h has all my core declarations in it like my main globals. main.cpp has all my window initilization functions and my winproc loop.
3
15957
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 loop. Is there some way to send a break from VBA code to break out of the loop? Here's what the error handler code looks like: Private Function MyFunction On Error GoTo HandleErr
12
2214
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 one: Private Sub SunMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SunMenuItem.Click If SunMenuItem.Checked Then SunMenuItem.Checked = False Else SunMenuItem.Checked = True
1
3478
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 below, I have one ASP.NET page that has a foreach loop, and inside this foreach loop, I was to spawn a new browser window using the second ASP.NET page. This doesn't work. Only the last time through the foreach loop causes a new browser window...
10
2266
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 when starting e.g. 5 ajax requests parallele. how to do that correctly? for one case, where I used *different* resp. handlers, I solved the problem by global variables - but now I have a loop of e.g. 20 calls to the *same* r.handler and I don*t...
0
9643
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
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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
9946
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
8968
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...
0
6735
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.