Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Question about loops

Reply
Thread Tools

Question about loops

 
 
alkalsa@gmail.com
Guest
Posts: n/a
 
      01-12-2007
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?

 
Reply With Quote
 
 
 
 
Petra Meier
Guest
Posts: n/a
 
      01-12-2007
function test34(){
// bla
}

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

(E-Mail Removed) 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?


 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      01-12-2007
(E-Mail Removed) 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

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      01-12-2007
http://www.velocityreviews.com/forums/(E-Mail 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[i]();
}
</script>
done
</body>
</html>


--

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Test::Unit Newbie Question regarding loops Yotta Meter Ruby 12 02-23-2010 11:47 PM
Newbie Question about multiple system calls within loops student.matt@gmail.com C Programming 23 07-01-2008 11:03 AM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
MS Perl question -- how to use hacked script to work correctly(was Question on loops and return values or sumpin) James Perl Misc 12 12-20-2004 04:15 PM
Question on loops and return values delfuego Perl Misc 12 12-09-2004 11:30 PM



Advertisments