Re: How to pass a parameter via an image.onload function call?

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

    #1

    Re: How to pass a parameter via an image.onload function call?

    Tuxedo schreef:
    However, this doesn't completely work, because the function
    photo_preloaded (PARAMETER) will run regardless of whether the image
    finished preloading or not. In normal cases, I do this kind of preloading
    without parameters, which works fine, as follows:
    >
    You can check the Image.complete property; example:

    img = new Image(100,100);
    interval = setInterval('ch eckcomplete()', 100);
    img.src = 'someimg';

    function checkcomplete() {
    if (window['img'].complete) {
    alert(1);
    clearInterval(w indow['interval']);
    }
    }


    JW

  • Thomas 'PointedEars' Lahn

    #2
    Re: How to pass a parameter via an image.onload function call?

    Janwillem Borleffs wrote:
    Tuxedo schreef:
    >However, this doesn't completely work, because the function
    >photo_preloade d(PARAMETER) will run regardless of whether the image
    >finished preloading or not. In normal cases, I do this kind of preloading
    >without parameters, which works fine, as follows:
    >
    You can check the Image.complete property;
    But you should not. That property is not even remotely interoperable.
    Instead, use the `onload' event handler:

    var img = new Image(...);
    img.onload = function() {
    this.onload = null;
    foo();
    };
    img.src = "...";
    example:
    >
    img = new Image(100,100);
    Always declare your identifiers.
    interval = setInterval('ch eckcomplete()', 100);
    setInterval() is defined as a method of Window objects, not of the Global
    Object. Should be

    window.setInter val(...)

    However, window.setInter val() is error-prone and has a tendency to cause
    extensive CPU load, especially with this short an interval (100 ms).
    Repeated checking like this should be only the last resort.
    img.src = 'someimg';
    >
    function checkcomplete() {
    if (window['img'].complete) {
    Never rely on that references to element objects can be retrieved through
    same-named properties of `window' or the Global Object alone.

    This will continue forever (and may accumulate timeouts, given enough DOM
    action) if the `complete' property is unsupported (see above).
    alert(1);
    clearInterval(w indow['interval']);
    You should not rely on that the object referred to by `window' is the
    Variable Object of an execution context. Another property lookup is also
    unnecessary here, the identifier lookup along the scope chain can take care
    of it.
    }
    }
    JFTR (the better solution is on the top):

    var img = new Image(100, 100);
    var interval = window.setInter val('checkcompl ete()', 100);
    img.src = 'someimg';

    function checkcomplete()
    {
    if (document.image s['img'].complete)
    {
    window.alert(1) ;
    window.clearInt erval(interval) ;
    }
    }


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann

    Comment

    • sasuke

      #3
      Re: How to pass a parameter via an image.onload function call?

      On Oct 24, 1:07 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
      wrote:
      window.setInter val(...)
      >
      However, window.setInter val() is error-prone and has a tendency to cause
      extensive CPU load, especially with this short an interval (100 ms).
      Repeated checking like this should be only the last resort.
      Interesting; any links which discuss in detail the `error prone'
      nature of window.setInter val()?

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: How to pass a parameter via an image.onload function call?

        sasuke wrote:
        Thomas 'PointedEars' Lahn wrote:
        > window.setInter val(...)
        >>
        >However, window.setInter val() is error-prone and has a tendency to cause
        >extensive CPU load, especially with this short an interval (100 ms).
        >Repeated checking like this should be only the last resort.
        >
        Interesting; any links which discuss in detail the `error prone'
        nature of window.setInter val()?
        There are plenty of them in Google Groups, which you are using.


        PointedEars
        --
        Prototype.js was written by people who don't know javascript for people
        who don't know javascript. People who don't know javascript are not
        the best source of advice on designing systems that use javascript.
        -- Richard Cornford, cljs, <f806at$ail$1$8 300dec7@news.de mon.co.uk>

        Comment

        • Jorge

          #5
          Re: How to pass a parameter via an image.onload function call?

          On Oct 24, 6:58 pm, sasuke <database...@gm ail.comwrote:
          On Oct 24, 1:07 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
          wrote:
          >
            window.setInter val(...)
          >
          However, window.setInter val() is error-prone and has a tendency to cause
          extensive CPU load, especially with this short an interval (100 ms).
          Repeated checking like this should be only the last resort.
          >
          Interesting; any links which discuss in detail the `error prone'
          nature of window.setInter val()?
          It's not error-prone. It's just that *some* people don't understand
          it.

          If code sets a setInterval(f, n), the browser will keep pushing into
          the timers' queue a call to f every n ms, regardless of whether the
          queue is being serviced or not: remember: the browser is single-
          threaded, and if it happens to be busy for a period of time longer
          than n ms, the programmed calls can't/won't be executed yet. But
          sooner or later the timer's queue will be serviced again and by then
          there might be there hundreds or even thousands of queued calls to f
          that will suddenly happen one after the other storm-like in a row
          until the queue gets emptied again.

          --
          Jorge.

          Comment

          • sasuke

            #6
            Re: How to pass a parameter via an image.onload function call?

            On Oct 25, 2:20 am, Jorge <jo...@jorgecha morro.comwrote:
            On Oct 24, 6:58 pm, sasuke <database...@gm ail.comwrote:
            >
            On Oct 24, 1:07 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
            wrote:
            >
              window.setInter val(...)
            >
            However, window.setInter val() is error-prone and has a tendency to cause
            extensive CPU load, especially with this short an interval (100 ms).
            Repeated checking like this should be only the last resort.
            >
            Interesting; any links which discuss in detail the `error prone'
            nature of window.setInter val()?
            >
            It's not error-prone. It's just that *some* people don't understand
            it.
            >
            If code sets a setInterval(f, n), the browser will keep pushing into
            the timers' queue a call to f every n ms, regardless of whether the
            queue is being serviced or not: remember: the browser is single-
            threaded, and if it happens to be busy for a period of time longer
            than n ms, the programmed calls can't/won't be executed yet. But
            sooner or later the timer's queue will be serviced again and by then
            there might be there hundreds or even thousands of queued calls to f
            that will suddenly happen one after the other storm-like in a row
            until the queue gets emptied again.
            Ah, thanks for the explanation; this might just explain the erratic
            nature of `setInterval'.

            /sasuke

            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: How to pass a parameter via an image.onload function call?

              sasuke wrote:
              On Oct 25, 2:20 am, Jorge <jo...@jorgecha morro.comwrote:
              >On Oct 24, 6:58 pm, sasuke <database...@gm ail.comwrote:
              >>On Oct 24, 1:07 am, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
              >>wrote:
              >>> window.setInter val(...)
              >>>However, window.setInter val() is error-prone and has a tendency to cause
              >>>extensive CPU load, especially with this short an interval (100 ms).
              >>>Repeated checking like this should be only the last resort.
              >>Interesting ; any links which discuss in detail the `error prone'
              >>nature of window.setInter val()?
              >It's not error-prone. It's just that *some* people don't understand
              >it.
              >>
              >If code sets a setInterval(f, n), the browser will keep pushing into
              >the timers' queue a call to f every n ms, regardless of whether the
              >queue is being serviced or not: remember: the browser is single-
              >threaded, and if it happens to be busy for a period of time longer
              >than n ms, the programmed calls can't/won't be executed yet. But
              >sooner or later the timer's queue will be serviced again and by then
              >there might be there hundreds or even thousands of queued calls to f
              >that will suddenly happen one after the other storm-like in a row
              >until the queue gets emptied again.
              >
              Ah, thanks for the explanation; this might just explain the erratic
              nature of `setInterval'.
              Don't listen to this wannabe.


              PointedEars
              --
              Anyone who slaps a 'this page is best viewed with Browser X' label on
              a Web page appears to be yearning for the bad old days, before the Web,
              when you had very little chance of reading a document written on another
              computer, another word processor, or another network. -- Tim Berners-Lee

              Comment

              Working...