Question about loops

A

alkalsa

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? :)
 
P

Petra Meier

function test34(){
// bla
}

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

RobG

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();
 
L

Lee

(e-mail address removed) 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();
}
</script>
done
</body>
</html>


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top