Advertisement
AtZako

GC of Images

Dec 10th, 2013
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.08 KB | None | 0 0
  1. <body>
  2.  
  3. <script>
  4. (function () {
  5.     var src = "https://www.gravatar.com/avatar/264484e7905f0381280e3d8e83a4c17a?s=32&d=identicon&r=PG";
  6.     var imagesToLoad = 1000;
  7.  
  8.     function onAllLoaded(img) {
  9.         console.log("All images are loaded");
  10.         console.time("All images are loaded");
  11.  
  12.         console.time("GC - 1st attempt");
  13.         gc();
  14.  
  15.         img.src = "https://www.gravatar.com/avatar/1e209e56c63bd72adb0d038377dde562?s=24&d=identicon&r=PG";
  16.         img = null;
  17.  
  18.         setTimeout(function () {
  19.             console.time("GC - 2nd attempt");
  20.             gc();
  21.         }, 1000);
  22.     }
  23.  
  24.     function createImg(i) {
  25.         var img = document.createElement("img");
  26.         img.dataset.index = i;
  27.  
  28.         img.onload = function () {
  29.             console.log("img loaded", this.dataset.index);
  30.             imagesToLoad--;
  31.             if (imagesToLoad === 0) {
  32.                 onAllLoaded(this);
  33.             }
  34.         };
  35.         img.onerror = function () {
  36.             console.log("img error", this.dataset.index);
  37.             imagesToLoad--;
  38.             if (imagesToLoad === 0) {
  39.                 onAllLoaded(this);
  40.             }
  41.         };
  42.  
  43.         img.src = src;
  44.         img = null;
  45.     }
  46.  
  47.     for (var i = 0; i < imagesToLoad; i++) {
  48.         createImg(i);
  49.     }
  50. })();
  51. </script>
  52.  
  53. </body>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement