Note that there are some explanatory texts on larger screens.

plurals
  1. POVariable scope issue in JavaScript
    text
    copied!<p>I have quickly coded up a sort of product display thing that gets half of its input from the page, and the other half from an AJAX query.</p> <p>Here is the code...</p> <pre><code>function productDisplay() { products = []; this.index = 0; setupProductDisplay(); processListItems(); showProduct(); 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'); var index = $('#products &gt; li').index($thisListItem); this.index = index; showProduct(); }); }; function showProduct() { var index = this.index; console.log('INDEX = ' + index); // hide current data $('#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){ var file = ''; var images = []; file = data.file; images = data.images; // show file list item if there is a file if (file) { $('#spex-sheet').show().find('a').attr( { href: downloadPath + file } ); } else { $('#spex-sheet').hide(); } // image gallery if (images.length != 0) { $('#product-gallery').show(); // preload image thumbnails $.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]} ) } console.log(images); // setup gallery var currentImage = 0; clearInterval(cycle); console.log(cycle); var cycle = setInterval(function() { console.log(currentImage + ' = ' + index); if (currentImage == images.length - 1) { currentImage = 0; } else { 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); // setup lightbox $("#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)); }); } else { // no images $('#enlarge-image').hide(); $('#product-gallery').hide(); }; // show the product info $('#product-info') .find('.hide-me') .remove('#product-gallery, #spex-sheet') .show() .parent() .find('.loading') .hide(); }); }; }; </code></pre> <p>The important function is showProduct(). Now generally I don't write JS like this, but I decided to give it a go. My problem is, that when a user clicks a 'more' button, and it displays the prouduct, it doesn't reset the simple slideshow (the images var is reset, I think it has to do with the setInterval() maybe, or it seems it's making a new instance of showProduct() everytime).</p> <p>Does anyone know what I'm doing wrong?</p>
 

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