Does this make sense?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adrian  MacNair

    Does this make sense?

    Because it's not working...

    I want a condition where if only the images are all preloaded then the slide
    show will work:

    <script type="text/javascript">
    var imagesdone=fals e;
    window.onload=l oadimages;
    function loadimages() {
    var images=new Array();
    for (var i=0;i<myImages. length;i++) {
    images[i]=new Image();
    images[i].src=myImages[i];
    }
    imagesdone=true ;
    }
    // This is an onClick event
    function startslideshow( ) {
    if (imagesdone) {
    do slideshow stuff
    } else {
    alert('Sorry, images are not loaded for viewing yet')
    }
    }
    </script>


  • Randy Webb

    #2
    Re: Does this make sense?

    Adrian MacNair wrote:[color=blue]
    > Because it's not working...
    >
    > I want a condition where if only the images are all preloaded then the slide
    > show will work:
    >
    > <script type="text/javascript">
    > var imagesdone=fals e;
    > window.onload=l oadimages;
    > function loadimages() {
    > var images=new Array();
    > for (var i=0;i<myImages. length;i++) {
    > images[i]=new Image();
    > images[i].src=myImages[i];
    > }
    > imagesdone=true ;[/color]

    That doesn't ensure the images are loaded, it only ensures that the
    request was made for the image. The imagesdone is set to true before the
    first image is even downloaded.


    Have the images preloaded while the page loads, then the window.onload
    won't fire until they are loaded. Another possibility is to add an
    onload to each preloaded image that will call a function that
    incremements a counter, then checks the counter. If the counter matches
    myImages.length then they are all loaded and it will set imagesdone to true.
    [color=blue]
    > }
    > // This is an onClick event
    > function startslideshow( ) {
    > if (imagesdone) {
    > do slideshow stuff
    > } else {
    > alert('Sorry, images are not loaded for viewing yet')
    > }
    > }
    > </script>
    >
    >[/color]


    --
    Randy
    comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly

    Comment

    • alu

      #3
      Re: Does this make sense?

      Another possibility is to write the images into the page but not display
      them; the <body> onload would fire only when all images are loaded.
      -alu


      <script type="text/javascript">

      var imagesdone=fals e;

      // This is an onClick event
      function startslideshow( ) {
      if (imagesdone) {
      alert("do slide show") ;
      } else {
      alert('Sorry, images are not loaded for viewing yet')
      }
      }
      </script>


      </head>
      <body onload="imagesd one=true;">

      <a href="#" onclick="starts lideshow()">tes t images loaded?</a>

      <script>
      for (var im=0;im<myImage s.length;im++) {
      document.write( '<img src="'+myImages[im]+'" alt=""
      style="display: none">')
      }

      </script>

      </body>


      Comment

      Working...