473,387 Members | 1,863 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,387 software developers and data experts.

Question about loops

Hi, I want to have something like this:

function callme1() {
alert('somestuff');
}

function callme2() {
alert('somestuff');
}

function callme3() {
alert('somestuff');
}

function callme4() {
alert('somestuff');
}

etc.

except, it's going to be created in a loop, like

var x=0;
while (x<10) {

function callme[x]() {
alert('somestuff');
}

x++
}

So, out of that I would like to get 10 callme(1-10 or A-J) functions...
I guess I am lost which way to build this with the placement & parsing
of the variables. Could someone please help me with guidence? :-)

Jan 12 '07 #1
3 972
function test34(){
// bla
}

call it with
test34()
or
self['test34']()
or in your case
self['test'+34]()

alka...@gmail.com schrieb:
Hi, I want to have something like this:

function callme1() {
alert('somestuff');
}

function callme2() {
alert('somestuff');
}

function callme3() {
alert('somestuff');
}

function callme4() {
alert('somestuff');
}

etc.

except, it's going to be created in a loop, like

var x=0;
while (x<10) {

function callme[x]() {
alert('somestuff');
}

x++
}

So, out of that I would like to get 10 callme(1-10 or A-J) functions...
I guess I am lost which way to build this with the placement & parsing
of the variables. Could someone please help me with guidence? :-)
Jan 12 '07 #2
alka...@gmail.com wrote:
Hi, I want to have something like this:

function callme1() {
alert('somestuff');
}

function callme2() {
alert('somestuff');
}
[...]
except, it's going to be created in a loop, like

var x=0;
while (x<10) {

function callme[x]() {
alert('somestuff');
}

x++
}

So, out of that I would like to get 10 callme(1-10 or A-J) functions...
I guess I am lost which way to build this with the placement & parsing
of the variables. Could someone please help me with guidence? :-)
I have no idea why you want to do this - whatever the reason is, there
is certainly a better way.

But anyhow...

for (var i=0; i<10; i++){
window['callme' + i] = function() {
alert('somestuff');
}
}

callme0();
callme1();
callme2();
...
callme9();

--
Rob

Jan 12 '07 #3
Lee
al*****@gmail.com said:
>So, out of that I would like to get 10 callme(1-10 or A-J) functions...
I guess I am lost which way to build this with the placement & parsing
of the variables. Could someone please help me with guidence? :-)
In addition to the other suggestions, you could create an array
of functions. I suspect that you would be better off calling a
single function with different arguments, though. It might help
if you told us what you're really trying to do.

<html>
<body>
<script type="text/javascript">
var callme = [
function() { alert("alpha") },
function() { alert("beta") },
function() { alert("gamma") },
function() { alert("delta") }
];

for ( var i=0; i<callme.length; i++) {
callme[i]();
}
</script>
done
</body>
</html>
--

Jan 12 '07 #4

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

Similar topics

14
by: 2mc | last post by:
Generally speaking, if one had a list (from regular Python) and an array (from Numerical Python) that contained the same number of elements, would a While loop or a For loop process them at the...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
8
by: Együd Csaba | last post by:
Hi All, how can I improve the query performance in the following situation: I have a big (4.5+ million rows) table. One query takes approx. 9 sec to finish resulting ~10000 rows. But if I run...
8
by: John Salerno | last post by:
Ok, for those who have gotten as far as level 2 (don't laugh!), I have a question. I did the translation as such: import string alphabet = string.lowercase code = string.lowercase + 'ab'...
19
by: Zach Heath | last post by:
I'm just starting C and haven't done programming for a few years...could you guys please take a look at this? Thanks for your time! I have an input file that looks like: 1.5 2.5 Bob, Joe...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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.