Skip to content

Instantly share code, notes, and snippets.

@steveluscher
Forked from eikes/imgpreload.js
Last active August 29, 2015 14:24
Show Gist options
  • Save steveluscher/847ff44423b71a1d2eae to your computer and use it in GitHub Desktop.
Save steveluscher/847ff44423b71a1d2eae to your computer and use it in GitHub Desktop.

Revisions

  1. steveluscher renamed this gist Jul 5, 2015. 1 changed file with 0 additions and 0 deletions.
  2. steveluscher revised this gist Jul 5, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion preloadImage(imgUrls, callback)
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ function preloadImages(imgUrls, callback) {
    // Some browsers fire onload events whenever an animated
    // GIF restarts a loop, so remove the on* handlers now.
    this.onabort = this.onerror = this.onload = null;
    if (numLoaded === imgUrls.length && callback) {
    if (callback && numLoaded === imgUrls.length) {
    callback(images);
    }
    };
  3. steveluscher renamed this gist Jul 5, 2015. 1 changed file with 22 additions and 13 deletions.
    35 changes: 22 additions & 13 deletions imgpreload.js → preloadImage(imgUrls, callback)
    Original file line number Diff line number Diff line change
    @@ -20,22 +20,31 @@
    * IN THE SOFTWARE.
    */

    function imgpreload( imgs, callback ) {
    function preloadImages(imgUrls, callback) {
    "use strict";
    var loaded = 0;
    var images = [];
    imgs = Object.prototype.toString.apply( imgs ) === '[object Array]' ? imgs : [imgs];
    var inc = function() {
    loaded += 1;
    if ( loaded === imgs.length && callback ) {
    callback( images );
    var numLoaded = 0;
    imgUrls = Object.prototype.toString.call(imgUrls) === '[object Array]'
    ? imgUrls
    : [imgUrls];
    var handleLoad = function() {
    numLoaded++;
    // Some browsers fire onload events whenever an animated
    // GIF restarts a loop, so remove the on* handlers now.
    this.onabort = this.onerror = this.onload = null;
    if (numLoaded === imgUrls.length && callback) {
    callback(images);
    }
    };
    for ( var i = 0; i < imgs.length; i++ ) {
    images[i] = new Image();
    images[i].onabort = inc;
    images[i].onerror = inc;
    images[i].onload = inc;
    images[i].src = imgs[i];
    var image;
    for (var i = 0; i < imgUrls.length; i++) {
    image = images[i] = new Image();
    image.onabort = image.onerror = image.onload = handleLoad;
    image.src = imgUrls[i];
    if (image.complete) {
    // Image is likely cached. Fire the handler right away.
    image.onabort = image.onerror = image.onload = null;
    handleLoad.call(image);
    }
    }
    }
  4. @eikes eikes revised this gist Oct 20, 2012. 1 changed file with 35 additions and 13 deletions.
    48 changes: 35 additions & 13 deletions imgpreload.js
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,38 @@
    function imgpreload( imgs, callback ) {
    "use strict";
    var loaded = 0;
    var images = [];
    imgs = Object.prototype.toString.apply( imgs ) === '[object Array]' ? imgs : [imgs];
    var inc = function() {
    loaded += 1;
    if ( loaded === imgs.length && callback ) {
    callback( images );
    }
    };
    for ( var i = 0; i < imgs.length; i++ ) {
    images[i] = new Image();
    /*
    * Copyright (C) 2012 Eike Send
    *
    * Permission is hereby granted, free of charge, to any person obtaining a copy
    * of this software and associated documentation files (the "Software"), to
    * deal in the Software without restriction, including without limitation the
    * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    * sell copies of the Software, and to permit persons to whom the Software is
    * furnished to do so, subject to the following conditions:
    *
    * The above copyright notice and this permission notice shall be included in
    * all copies or substantial portions of the Software.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    * IN THE SOFTWARE.
    */

    function imgpreload( imgs, callback ) {
    "use strict";
    var loaded = 0;
    var images = [];
    imgs = Object.prototype.toString.apply( imgs ) === '[object Array]' ? imgs : [imgs];
    var inc = function() {
    loaded += 1;
    if ( loaded === imgs.length && callback ) {
    callback( images );
    }
    };
    for ( var i = 0; i < imgs.length; i++ ) {
    images[i] = new Image();
    images[i].onabort = inc;
    images[i].onerror = inc;
    images[i].onload = inc;
  5. @eikes eikes created this gist Oct 20, 2012.
    19 changes: 19 additions & 0 deletions imgpreload.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    function imgpreload( imgs, callback ) {
    "use strict";
    var loaded = 0;
    var images = [];
    imgs = Object.prototype.toString.apply( imgs ) === '[object Array]' ? imgs : [imgs];
    var inc = function() {
    loaded += 1;
    if ( loaded === imgs.length && callback ) {
    callback( images );
    }
    };
    for ( var i = 0; i < imgs.length; i++ ) {
    images[i] = new Image();
    images[i].onabort = inc;
    images[i].onerror = inc;
    images[i].onload = inc;
    images[i].src = imgs[i];
    }
    }