title: javascript can I dynamically build a function name to call?
body: <p>I have functions called</p>
  
  <pre><code>BeforeSlide1() {
  ...
  }
  
  
  BeforeSlide2() {
  ...
  }
  </code></pre>
  
  <p>And I would like to be able to build the call to these dynamically in a loop, something like this:</p>
  
  <pre><code>        code = "BeforeSlide" + slide + "()"
          eval(code); 
  </code></pre>
  
  <p>Now this code of mine obviously doesn't work (if only it was that simple!) anyone know?</p>
  
  <p>All right, I'll draw up the big picture.</p>
  
  <p>I have a slideshow run by a simple script</p>
  
  <pre><code>    function slideshow(slide) {
              if (pauseTimes[slide]&gt;0) {
                      /* if there is another slide after the one currently showing... */
  
                  //Call BeforeSlide-function if there is one
                  code = "BeforeSlide" + slide + "();"
                  try {
                      eval(code);                 
  
                  } catch(e) {
                      /* do nothing since many slides don't have one */
                  }           
                  $('#slide'+slide).
                  fadeIn(1500,function(){}).
                  delay(pauseTimes[slide]).
                  fadeOut(1500,function(){slideshow(slide+1);});
                  ;
              }
              else {
                      /* if there is no more slide and we've reached the end, try to ajax-refresh the slideshow contents */
                  ajaxUpdate();
              }
              }
  </code></pre>
  
  <p>Among the HTML of the slides themselves, the before- and afterfunctions are defined like this:</p>
  
  <pre><code>    /* fire the slideshow from slide 1 once document is finished loading */
      $(document).ready(function(){
          slideshow(1);
      });
  
      &lt;/script&gt;
  
      &lt;/HEAD&gt;
  
      &lt;body id="slideshow_body"&gt;
  
      &lt;script id='pauseTimesScript' type='text/javascript'&gt;
      /* Define pauseTimes (array holding duration of each slide) */
      try {
              pauseTimes=null;
              pauseTimes=undefined;
              pauseTimes = new Array();   
              setPauseTimes();        
              /*alert('satte först till null, deklarerade sedan om.');*/
      } catch(e) {
              pauseTimes = new Array();   
              setPauseTimes();        
              /*alert('deklarerade utan att först sätta null.');*/
      }
  
      function setPauseTimes() {  
  
          pauseTimes[1]=2000;         
  
          pauseTimes[2]=2000;         
  
          pauseTimes[3]=2000;         
  
      }
      &lt;/script&gt;
  
      &lt;div id="avbrott"&gt;Tillfälligt avbrott, visar cachelagrat innehåll sedan 2012-10-31 16:37:35&lt;/div&gt;
  
      &lt;div id="canvas" style="width:1072px;height:732px;"&gt;
  
                  &lt;script language='text/javascript'&gt;
                  function BeforeSlide1(order=1) {
                  alert('here goes slide '+order);
  
                  }
                  &lt;/script&gt;
                          &lt;div id="slide1" class="slide" style="z-index:1;"&gt;
  
  
  
      &lt;div id="slide_bg" style="float:left;height:100%;width:100%;background-color:#ffffff;background-image:url('http://bglabs.evade.netdna-cdn.com/45875kli90/505.jpg');"&gt;
  
          &lt;div style="background-color:#eeeeee;color:#000000;float:right;text-align:center;margin-top:30px;padding:10px;font-weight:bold;font-size:30pt;" id="preview_title"&gt;Egypt rocks!&lt;/div&gt;
          &lt;div style="clear:both;float:none;"&gt;&lt;/div&gt;
  
          &lt;p style="color:#000000;margin:10px 10px 0 20px;font-style:italic;font-size:38pt;" id="preview_data_6"&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse feugiat pharetra eros, vitae ullamcorper tellus condimentum sit amet. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse in leo erat. Aliquam interdum molestie tortor, nec lacinia mauris viverra eget. Suspendisse aliquam tempor scelerisque. Proin vel purus nunc. Pellentesque ut sapien libero.&lt;br /&gt;
      &lt;/p&gt;
  
      &lt;/div&gt;
  </code></pre>
  
  <p>The purpose of this BeforeSlide function is of course to do other stuff than produce an alert...</p>
  
  <p>Please let me know how I should structure it in a better way! =)</p>