Stop do while loop - image preloader

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

    Stop do while loop - image preloader

    Hi everyone,

    I am trying to stop an image preload sequence by the click of a mouse but
    have been unsuccessful trying several methods. Imagine this simple script
    below that loads 50 images to cache. If the stopPreload() function is
    activated and the ret val set to false, the preload() function still
    continues to the end.

    Any suggestions on how to stop the preload() function in its process, what
    conditions are necessary?

    var ret=true;
    function preload(){
    var k=1;
    do {
    theImageSrc="im age"+k+".jpg";
    document.imgArr y[k]=new Image;
    document.imgArr y[k].src=theImageSr c;
    k++;
    }
    while (ret==true && k < 50)
    }
    }

    function stopPreload(){
    ret=false;
    }

    Thanks, David


  • Baconbutty

    #2
    Re: Stop do while loop - image preloader

    If I understand you correctly, you want to be able to stop the loop in
    response to an onclick event.

    As far as I understand things (others will no doubt correct me/express
    it more accurately) as a general rule, once a function starts running,
    the browser window will not respond to any further events until the
    function has completed and returned.

    Accordingly you cannot (without some fancy foot-work) generally stop a
    function by means of a user onclick event once it has begun.

    When you click to change ret val, the stopPreload function does not
    actually run until preLoad has finished.

    The only way I have found (as an amateur) to change this is quite
    complicated, and is based on the fact that whilst one "window" may be
    unresponsive, others will not be.

    1. Make "ret" the property of an object.

    var oRet={"ret":tru e};

    while (oReg.ret==true )

    2. Before commencing preLoad, open up a dialogue box or iframe (i.e.
    a new "window"), which contains the "cancel" button, and pass the oRet
    object as a reference.

    3. The new window object will detect the onclick event, and can
    update the oRet object, which then feeds back to the preLoad function.

    I have used this approach before to create progress bars with cancel
    buttons as separate dialogue boxes.




    2.

    Comment

    • David

      #3
      Re: Stop do while loop - image preloader


      "Baconbutty " <julian@baconbu tty.com> wrote in message
      news:1123668512 .445836.299140@ g14g2000cwa.goo glegroups.com.. .[color=blue]
      > If I understand you correctly, you want to be able to stop the loop in
      > response to an onclick event.
      >
      > As far as I understand things (others will no doubt correct me/express
      > it more accurately) as a general rule, once a function starts running,
      > the browser window will not respond to any further events until the
      > function has completed and returned.
      >
      > Accordingly you cannot (without some fancy foot-work) generally stop a
      > function by means of a user onclick event once it has begun.
      >
      > When you click to change ret val, the stopPreload function does not
      > actually run until preLoad has finished.
      >
      > The only way I have found (as an amateur) to change this is quite
      > complicated, and is based on the fact that whilst one "window" may be
      > unresponsive, others will not be.
      >
      > 1. Make "ret" the property of an object.
      >
      > var oRet={"ret":tru e};
      >
      > while (oReg.ret==true )
      >
      > 2. Before commencing preLoad, open up a dialogue box or iframe (i.e.
      > a new "window"), which contains the "cancel" button, and pass the oRet
      > object as a reference.
      >
      > 3. The new window object will detect the onclick event, and can
      > update the oRet object, which then feeds back to the preLoad function.
      >
      > I have used this approach before to create progress bars with cancel
      > buttons as separate dialogue boxes.[/color]


      That's what I thought about the function prosess not being able to be
      interupted from another function. At least that's my experience in trying to
      get it to stop. Your suggestion sounds good but unfortunately opening a new
      "window" isn't an option.

      Thanks for the suggestion, David





      Comment

      • Grant Wagner

        #4
        Re: Stop do while loop - image preloader

        As already explained, you are in a tight loop, it is unlikely the
        browser will respond to any events while executing this code. There is
        also a matter of whether JavaScript is multithreaded and can actually
        run the stopPreload() function while preload() is executing.

        You should be able to make this work, but it will require the use of
        setTimeout() or setInterval(), which will slow down your preload
        sequence.

        var preloadTimer= setInterval(pre load, 10);
        function preload() {
        if ('undefined' == typeof loop) {
        loop = 0;
        }
        document.imgArr y[loop] = new Image();
        document.imgArr y[loop].src = "image" + loop + ".jpg";
        if (++loop >= 50) {
        clearInterval(p reloadTimer);
        }
        }
        function stopPreload() {
        if (preloadTimer) {
        clearInterval(p reloadTimer);
        }
        }

        Comment

        • David

          #5
          Re: Stop do while loop - image preloader

          > As already explained, you are in a tight loop, it is unlikely the[color=blue]
          > browser will respond to any events while executing this code. There is
          > also a matter of whether JavaScript is multithreaded and can actually
          > run the stopPreload() function while preload() is executing.
          >
          > You should be able to make this work, but it will require the use of
          > setTimeout() or setInterval(), which will slow down your preload
          > sequence.[/color]

          Grant,

          Thanks, this is interesting. I don't mind a little drag on the preload, just
          need to 'pause' or stop it so that I can load an image, that is currently
          preloading, that has not yet cached. Otherwise, the image I am trying to
          display has to wait until it has been cached by the preloader.

          Once this image has been displayed "instantly" or the time it would take to
          cache this image only, I can resume the preload, or start it over again.

          I'll chew on your example. It looks promising.

          David


          Comment

          Working...