Question about loops

Discussion in 'Javascript' started by alkalsa@gmail.com, Jan 12, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Petra Meier Guest

    function test34(){
    // bla
    }

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

    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? :)
    Petra Meier, Jan 12, 2007
    #2
    1. Advertising

  3. RobG Guest

    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
    RobG, Jan 12, 2007
    #3
  4. Lee Guest

    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>


    --
    Lee, Jan 12, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    23
    Views:
    622
    Joachim Schmitz
    Jul 1, 2008
  2. Yotta Meter
    Replies:
    12
    Views:
    157
    Richard Conroy
    Feb 23, 2010
  3. delfuego

    Question on loops and return values

    delfuego, Dec 3, 2004, in forum: Perl Misc
    Replies:
    12
    Views:
    232
    Michele Dondi
    Dec 9, 2004
  4. James
    Replies:
    12
    Views:
    814
    James
    Dec 20, 2004
  5. Me
    Replies:
    2
    Views:
    237
Loading...

Share This Page