Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had to reformat your code to really understand what was going on. Anyway, I found the problem with the code.</p> <p>As you guessed correctly, problem is with the scope but not with the variable 'images' but with variable 'cycle'. Why?</p> <p>This line</p> <pre><code>var cycle = setInterval(function() { </code></pre> <p>Always creates a new local cycle variable (notice the 'var') which is not accessible when showProduct gets called the second time. This means that this line</p> <pre><code>clearInterval(cycle); </code></pre> <p>is essentially useless as it always passes null to the clearInterval function and doesn't clear anything. This means that as you keep clicking on 'more', you are creating more and more setInterval function calls, never clearing the old ones. </p> <p>Anyway, I have refactored your code a little bit, I think this should work as expected. The changes I did are:</p> <ol> <li><p>Removed this.index variable. It's better to pass 'index' to showProduct instead of setting this.index before showProduct method call and making showProduct use that variable. Also, why did you prefix the variable with 'this'? </p></li> <li><p>Declared cycler variable outside the scope of showProduct, local to the productDisplay method. This insures that you can access cycler during different showProduct calls. </p></li> <li><p>Created smaller functions named showFile, showGallery, showProductInfo to make it easier to understand/maintain code. </p></li> </ol> <p>Let me know if you have any questions OR if the code still doesn't work.</p> <pre><code>function productDisplay() { //Instead of keeping this.index variable, it's better to make showProduct function //take index variable. products = []; setupProductDisplay(); processListItems(); //We have to define cycler outside the showProduct function so that it's maintained //in between showProduct calls. var cycler = null; showProduct(0); function setupProductDisplay() { var productInfoBoxHtml = '&lt;div id="product-info"&gt;&lt;h3 class="hide-me"&gt;&lt;/h3&gt;&lt;span id="dimensions" class="hide-me"&gt;&lt;/span&gt;&lt;div id="product-gallery"&gt;&lt;img alt="" src="" /&gt;&lt;/div&gt;&lt;ul id="product-options" class="hide-me"&gt;&lt;li id="spex-sheet"&gt;&lt;a href="" rel="external"&gt;Download full spex sheet&lt;/a&gt;&lt;/li&gt;&lt;li id="enlarge-image"&gt;&lt;a href="" rel="lightbox-gallery"&gt;Enlarge image&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div id="product-description" class="hide-me"&gt;&lt;/div&gt;&lt;span id="top"&gt;&lt;/span&gt;&lt;span id="bottom"&gt;&lt;/span&gt;&lt;span id="side"&gt;&lt;/span&gt;&lt;span class="loading"&gt;&lt;/span&gt;&lt;/div&gt;'; $('#products').after(productInfoBoxHtml); } function processListItems() { $('#products &gt; li') .append('&lt;span class="product-view"&gt;View&lt;/span&gt;') .filter(':even') .addClass('even') .end() .each( function() { products.push({ id: $(this).find('h3').html(), title: $(this).find('h3').html(), dimensions: $(this).find('.dimensions').html(), description: $(this).find('.product-description').html() }); }) .find('.product-view') .click( function() { var $thisListItem = $(this).parents('ul li'); showProduct($('#products &gt; li').index($thisListItem)); } ); }; function showFile(file) { if (file) { $('#spex-sheet').show().find('a').attr( { href: downloadPath + file } ); } else { $('#spex-sheet').hide(); } } function showGallery(images) { if(! images || !images.length || images.length == 0) { $('#enlarge-image').hide(); $('#product-gallery').hide(); return; } $('#product-gallery').show(); $.each(images, function(i, image) { var img = new Image(); img.src = imagePath + 'thumb-' + image; img = null; }); // set first image thumbail and enlarge link if (images[0]) { $('#enlarge-image').show().find('a').attr({ href: imagePath + images[0] }); $('#product-gallery img').attr ( { src: imagePath + 'thumb-' + images[0]} ) } var currentImage = 0; clearInterval(cycler); cycler = setInterval( function() { currentImage = currentImage == images.length - 1 ? 0 : currentImage++; var obj = $('#product-gallery'); var imageSource = imagePath + 'thumb-' + images[currentImage]; obj.css('backgroundImage','url(' + imageSource +')'); obj.find('img').show().fadeOut(500, function() { $(this).attr({src: imageSource}) }); $('#enlarge-image a').attr({ href: imagePath + images[currentImage] }); }, 5000); $("#enlarge-image a").slimbox({/* Put custom options here */}, null, function(el) { return (this == el) || ((this.rel.length &gt; 8) &amp;&amp; (this.rel == el.rel)); }); }; function showProductInfo() { $('#product-info') .find('.hide-me') .remove('#product-gallery, #spex-sheet') .show() .parent() .find('.loading') .hide(); } function showProduct(index) { $('#product-info') .show() .find('.hide-me, #product-gallery') .hide() .parent() .find('.loading') .show(); // get data contained in the page $('#product-info') .find('h3') .html(products[index].title) .parent() .find('#dimensions') .html(products[index].dimensions) .parent() .find('#product-description') .html(products[index].description) // get id &amp; then product extra info var id = $('#products &gt; li').eq(index).attr('id').replace(/id-/, ''); var downloadPath = PATH_BASE + 'downloads/'; var imagePath = PATH_BASE + 'images/products/' $.getJSON(PATH_BASE + 'products/get/' + id + '/', function(data) { showFile(data.file); showGallery(data.image); showProductInfo(); }); }; }; </code></pre>
 

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