Today I ran into a problem that had me completely baffled.
A function that I have just written, processes data in any one of 4
different data grids.
Within a for() loop I need to call another specific function depending
on the grid being accessed. To do this I construct the function name
from a variable and a literal string. No problem yet.
When I ran the web page that called the function, the for() loop kept
going into what appeared to be an infinite loop(?), but I wasn't
updating the counter variable - or so I thought.
Eventually, it dawned on me what was happening. The called funtions
also have a for() loop using the same counter variable (both "i").
While neither function should be aware of the other's variable as they
were both locally declared and therefore should have been out of
scope, the called function was updating the calling function's counter
variable.
This seems weird in the extreme, but it was happening with both
FireFox and Internet Explorer, so it seem to be a javascript "thing".
The problem disappeared of course after I changed the calling
function's counter variable.
Something to be aware of, folks! 3 4289
crater wrote on 02 feb 2008 in comp.lang.javas cript:
Today I ran into a problem that had me completely baffled.
A function that I have just written, processes data in any one of 4
different data grids.
Within a for() loop I need to call another specific function depending
on the grid being accessed. To do this I construct the function name
from a variable and a literal string. No problem yet.
When I ran the web page that called the function, the for() loop kept
going into what appeared to be an infinite loop(?), but I wasn't
updating the counter variable - or so I thought.
Eventually, it dawned on me what was happening. The called funtions
also have a for() loop using the same counter variable (both "i").
While neither function should be aware of the other's variable as they
were both locally declared and therefore should have been out of
scope, the called function was updating the calling function's counter
variable.
This seems weird in the extreme, but it was happening with both
FireFox and Internet Explorer, so it seem to be a javascript "thing".
The problem disappeared of course after I changed the calling
function's counter variable.
Something to be aware of, folks!
I would think it to be a simple programming error on your part,
not an error of the interpreting programme.
Limiting scope of a variable is one of the nice things of javascript,
painfully absent in some other languages.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
crater wrote:
Today I ran into a problem that had me completely baffled.
Eventually, it dawned on me what was happening. The called funtions
also have a for() loop using the same counter variable (both "i").
While neither function should be aware of the other's variable as they
were both locally declared and therefore should have been out of
scope, the called function was updating the calling function's counter
variable.
Something to be aware of, folks!
As the others have said, it's more likely an error in your code. Here's
the most common error in for loops:
function somefunc()
{
for(i=0;i<somet hing;i++)
{
dosomething();
}
}
Is that what your loop (basically) looked like ? If it does, then you're
missing the crucial var keyword that makes that i variable local to the
function. If you don't have it, you're declaring/sharing a global
variable. Here's how that look should look:
for(var i=0;i<something ;i++)
{
dosomething();
}
Is that what your loop (basically) looked like ? If it does, then you're
missing the crucial var keyword that makes that i variable local to the
function. If you don't have it, you're declaring/sharing a global
variable. Here's how that look should look:
for(var i=0;i<something ;i++)
{
* *dosomething();
}- Hide quoted text -
- Show quoted text -
Yes! You hit the nail on the head. I'd obviously developed tunnel
vision with this.
A nudge once in a while with a sharp elbow does wonders for clarity of
mind.
Many thanks. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: knocte |
last post by:
Hello.
I have always thought that the eval() function was very flexible and
useful. If I use it, I can define functions at runtime!!
However, I have found a case where eval() does not work properly. It
works, for example, when invoking functions (alert('hello')), but not
for defining functions.
The case occurs when retrieving the javascript code with
|
by: headware |
last post by:
I know that you can call the method of one from from inside another
form by doing something like this
Forms("MyForm").MyFunction(12, 34)
However, you have to know that MyForm has a function called
MyFunction. Can you specify a string for the function name like you
can with the form name? That is can I do something like
Forms("MyForm")."MyFunction"
|
by: Kostis |
last post by:
I have a form with 100 images (named from img1 to img100) and a Click
event associated to each one.
I want to write a function that calls these events.
I have tried the following:
Dim imgEvent as Variant
For i = 1 to 100
imgEvent = "img" & Format$(i) & "_Click" 'Create image name.
Call Eval(imgEvent)
|
by: Alex |
last post by:
I have a load of function names stored in a table which I want to use
to decide which function to run. I can select the function name but I
cannot work out how to call the function if I have the name of the
function as a string.
Normally in my code I would just call the function name but if I do
this with the string variable which is the function name I get an
error.
Can anyone help.
Alex
|
by: ratnakarp |
last post by:
Hi,
I have a search text box. The user enters the value in the text box and
click on enter button. In code behind on button click i'm writing the
code to get the values from the database and binding it to a repeater
control. This repeater control has multiple text boxes and buttons. Can
you please tell me how can i do paging in this case ?
I'm posting my code below. The problem is that if i click on
"AdjustThisAd" button, it opens...
| |
by: santel_helvis |
last post by:
Hi,
Here is my code.
function CallDisplay()
{
nodeobj=new mynode();
x="displayId("+nodeobj+")";
eval(x);
|
by: Pauljh |
last post by:
Hi All,
I'm running some javascript over a server side generated web page
and have multiple generated empty select statements, that I want to
populate when the page is loaded. As HTML doesn't do arrays each
select is individually named withe MySelecti where i is an incremental
from 1.
I know all my variables are correct (i, OptionsCount) and my arrays
of MyValues and MyDescription's exist for multiple enteries and if I
bring out an...
|
by: |
last post by:
I have what's probably a simple page lifecycle question related to
dynamically evaluating values that are placed by a repeater and dynmically
placing user controls that use those values.
I'm attempting to bind a user control I've written, "ImageBox", to a
repeater.
The user control takes a custom property, "ContentID", that will execute a
database lookup and load an image.
|
by: Darko |
last post by:
Hello,
I have this particular problem with eval() when using Microsoft
Internet Explorer, when trying to define an event handler. This is the
code:
function BigObject()
{
this.items = new Array();
this.values = new Array();
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |