Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>try this instead (it's all about the logic):</p> <pre><code>function preload(arrayOfImages) { $(arrayOfImages).each(function(){ $(this[0]).src = this[1]; }); $('body').removeClass("loading"); } $(function() { $('body').addClass("loading"); }; /// /// elsewhere /// ///I'm also updating your array call because it doesn't make much sense, sadly to me ///otherwise you're overwiting all images with the same image every time. ///I think it makes much more sense to have a set of hidden images in the /// footer of the page so the browser keeps them in memory/cache $(document).ready(function () { preload([ ['selector1','WCMSWR/_assets/images/images/1.png'], ['selector2','WCMSWR/_assets/images/images/2.png'], ['selector3','WCMSWR/_assets/images/images/3.png'] ]); }); </code></pre> <p>So, onload you're adding the loading class, and then once you've finished loading then you're removing the class as the last thing you do.</p> <p>Ostensibly, however, I would structure it more like this: (assuming a current release [>=1.8] of jQuery)</p> <pre><code>function loadSomeImages(arrayOfImages){ $.Deferred(function(){ $('body').addClass("loading"); }).then(function(){ $(arrayOfImages).each(function(){ $(this[0]).src = this[1]; }); }).done(function(){ $('body').removeClass("loading"); }).resolve(); } /// /// elsewhere /// $(document).ready(function () { preload([ ['selector1','WCMSWR/_assets/images/images/1.png'], ['selector2','WCMSWR/_assets/images/images/2.png'], ['selector3','WCMSWR/_assets/images/images/3.png'] ]); }); </code></pre> <p>And because you're using deferred's to wait till each thing is done, you're not tying up the jitter (yay IE bugs), your code is clean for what you <em>intend</em> to have happen, and you have ordered things to go in the right manner.</p> <p>Hopefully this did what you basically intended, and helps.</p> <hr> <p>I would also urge you to "namespace" your objects. I've extended the previous example to do so below:</p> <pre><code>window.sephiith = window.sephiith | {}; window.sephiith.loadSomeImagesDeferred = null; window.sephiith.loadSomeImages = function loadSomeImages(arrayOfImages){ sephiith.loadSomeImagesDeferred = $.Deferred(function _startArrayOfImages(){ $('body').addClass("loading"); }); sephiith.loadSomeImagesDeferred.then(function _applyArrayOfImages(){ $(arrayOfImages).each(function(){ $(this[0]).src = this[1]; }); }); sephiith.loadSomeImagesDeferred.done(function _doneArrayOfImages(){ $('body').removeClass("loading"); sephiith.loadSomeImagesDeferred = null; }); return sephiith.loadSomeImagesDeferred; } /// /// elsewhere /// $(document).ready(function () { preload([ ['selector1','WCMSWR/_assets/images/images/1.png'], ['selector2','WCMSWR/_assets/images/images/2.png'], ['selector3','WCMSWR/_assets/images/images/3.png'] ]); }).resolve(); </code></pre> <p>This namespacing ensures that your code doesn't interfere with "someone else's" code. While it may seem like overkill, over time this namespacing is a good direction to go in. You can also now see if <code>sephiith.loadSomeImagesDeferred</code> has a value in other places in your code, so you can know that you're still loading images, which can be helpful as well. Maybe now you want to do <em>something else</em> when this finishes?</p> <pre><code>if(sephiith.loadSomeImagesDeferred) /* sanity check to make sure we didn't already null it out */ { // TODO: track sephiith.loadSomeImagesDeferred to add a new function #NEWFUNCTION sephiith.loadSomeImagesDeferred.then(function _NEWFUNCTION(){ /* do more stuff here */ }); } else { console.log( 'sephiith.loadSomeImagesDeferred was already nulled out before #NEWFUNCTION!!' ); } </code></pre> <p>so you could build up all these deferred items as the page loads, so that when you hit the <code>loadSomeImages(things);</code> you will have all these items fire off before your done.</p> <p>I realize that <code>Deferred</code> may be a big new thing to bite off, so if it doesn't make sense, no worries, but this makes your code much more <em>intentful</em> of what you want to have happen. Also notice that I've made sure to tell myself that I wanted to add something else but that <code>#NEWFUNCTION</code> didn't get added in time. (also, always name your anonymous functions. It makes stacktrace tracking so much easier.) </p> <p>So, that last example actually got kind of complicated. I added an HR above it to make it easier to suss out where I "jumped the shark". <strong><em>And because I'm an anal dev who wants to make things cleaner as I go ...</em></strong> here's some more refinements, hopefully this code progression has made sense:</p> <hr> <pre><code>window.sephiith = window.sephiith | {}; sephiith.loadSomeImagesDeferred = null; sephiith._startArrayOfImages = function _startArrayOfImages(){ $('body').addClass("loading"); }; sephiith._doneArrayOfImages = function _doneArrayOfImages(){ $('body').removeClass("loading"); sephiith.loadSomeImagesDeferred = null; sephiith._ArrayOfImages = {}; }; sephiith._ArrayOfImages = {}; sephiith._applyArrayOfImages = function _applyArrayOfImages(){ $(sephiith._ArrayOfImages).each(function(){ $(this[0]).src = this[1]; }); }; sephiith.loadSomeImages = function loadSomeImages(arrayOfImages){ sephiith._ArrayOfImages = arrayOfImages; sephiith.loadSomeImagesDeferred = $.Deferred(sephiith._startArrayOfImages); sephiith.loadSomeImagesDeferred.then(sephiith._applyArrayOfImages); sephiith.loadSomeImagesDeferred.done(sephiith._doneArrayOfImages); return sephiith.loadSomeImagesDeferred; } /// /// elsewhere /// $(document).ready(function () { preload([ ['selector1','WCMSWR/_assets/images/images/1.png'], ['selector2','WCMSWR/_assets/images/images/2.png'], ['selector3','WCMSWR/_assets/images/images/3.png'] ]).resolve(); }); /// /// elsewhere /// notice we're still loading the page, so we haven't called the onready of the preload /// if(!sephiith._newFunction) { sephiith._newFunction = function _NEWFUNCTION(){ /* do more stuff here */ console.log(sephiith._ArrayOfImages); }; } else { console.log('sephiith._newFunction was already defined, not defining again'); } if(sephiith.loadSomeImagesDeferred) { // TODO: track sephiith.loadSomeImagesDeferred to add a new function #NEWFUNCTION sephiith.loadSomeImagesDeferred.then(sephiith._newFunction); } else { console.log( 'sephiith.loadSomeImagesDeferred was already nulled out before sephiith._newFunction could be added!!' ); } </code></pre> <hr> <p>postscript: I didn't test this, I wrote it basically from my workingspace based on what you wanted, and so I may have some bugs, but this should effectively do what you want.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload