title: Using jQuery Cycle and ajax to dynamically create a slide show - images don't load immediately
body: <p>I'm currently trying to make a cheap, simple slide show navigation box for a client (something along the lines of what, say, the Red Sox or Gamespot use on their sites, but far, far simpler).  So far, it's actually coming along nicely, with one problem - the images don't appear upon a first visit.  They only appear after the page is reloaded.  I think it may be some sort of runtime issue, or perhaps a cache issue, but I'm not sure how to fix it.  My code:</p>
  
  <p>PHP:</p>
  
  <pre><code>if (isset($_GET['start']) &amp;&amp; "true" === $_GET['start'])
  {
      $images = array();
  
      if ($dir = dir('images'))
      {
          //$count = 0;
  
          while(false !== ($file = $dir-&gt;read()))
          {
              if (!is_dir($file) &amp;&amp; $file !== '.' &amp;&amp; $file !== '..' &amp;&amp; (substr($file, -3) === 'jpg' || substr($file, -3) === 'png' || substr($file, -3) === 'gif'))
              {
                  $lastModified = filemtime("{$dir-&gt;path}/$file");
                  $images[$lastModified] = $file;
                  //$images["image$count"] = $file;
                  //++$count;
              }
          }
  
          echo json_encode($images);
      }
      else { echo "Could not open directory"; }
  }
  </code></pre>
  
  <p>HTML and JavaScript:</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html lang="en-us"&gt;
      &lt;head&gt;
          &lt;title&gt;jQuery Cycle test&lt;/title&gt;
          &lt;script type="text/javascript" src="js/jquery-1.4.2.min.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript" src="js/jquery.cycle.all.min.js"&gt;&lt;/script&gt;
          &lt;style&gt;
              #slideshow a { margin: 0; padding: 0; color: #fff; }
          &lt;/style&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;div id="slideshow"&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  
      &lt;script type="text/javascript"&gt;
          $.get('slideshow.php', {start : "true"}, function(data){
              var images = JSON.parse(data);
  
              for(var image in images){
                  $('#slideshow').append('&lt;a href="images/' + images[image] + '"&gt;&lt;img src="images/' + images[image] + '" alt="" /&gt;&lt;/a&gt;');
              }
  
              $('#slideshow').cycle({
                  fx: 'cover',
                  direction: 'right',
                  timeout: 3000,
                  speed: 300
              });
          });
      &lt;/script&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>I think I may need to delay the timing of the cycle function, or perhaps somehow force it to 'see' the images the first time through.  I'm just not sure how to do it.</p>