Looping through JSON data in jQuery and using correct Array Value
<p>I have the below code that I have "cobbled" together. Basically it should take some JSON data (a sample of which is also below), preload any applicable images and modify a slideshow to use the data passed back.</p>

<p>It is working, however the images and text isn't matching up, I presume it is something to do with the <code>currImg++%preloadArrayName.length</code> however I am not sure what this is signifying...</p>

<p>Any help gratefully appreciated!</p>

<pre><code>function updateHomepageSlideshow(data)
{
var preloadArr = new Array();
var preloadArrName = new Array();
var preloadArrOffer = new Array();
var i;

/* preload images */
for(i=0; i &lt; data.length; i++){
    preloadArr[i] = new Image();
    preloadArr[i].src = 'http://media.domain.com/restaurants/large/' + data[i]   ['restaurantCode'] + '.jpg';
    preloadArrName[i] = data[i]['restaurantName'];
    preloadArrOffer[i] = data[i]['offerName'];
}

var currImg = 1;
var intID = setInterval(changeImg(data), 6000);

/* image rotator */
function changeImg(data){
    $('#amazingOffersAt').hide();
    $('#homepageRestaurantNameId').html(preloadArrName[currImg++%preloadArrName.length]);
    $('#homepageRotatingImage').css('background-image','url(' + preloadArr[currImg++%preloadArr.length].src +')');
    $('#homepageRestaurantOfferId').html(preloadArrOffer[currImg++%preloadArrOffer.length]);
}
}
</code></pre>

<p><strong>The JSON</strong></p>

<pre><code>[{
"restaurantName": "Caf\u00e9 des Amis",
"restaurantCode": "cafe-des-amis",
"address": "11 - 14 Hanover Place",
"town": "Covent Garden",
"county": "London",
"postcode": "WC2E 9JP",
"lat": "51.5133900",
"lng": "-0.1231300",
"largeImage": "1",
"offerTypeId": "9",
"offerName": "3 course set menu for \u00a315"
}, {
"restaurantName": "Palm Court Brasserie",
"restaurantCode": "palm-court-brasserie",
"address": "39 King Street",
"town": "Covent Garden",
"county": "London",
"postcode": "WC2E 8JS",
"lat": "51.5119700",
"lng": "-0.1243000",
"largeImage": "1",
"offerTypeId": "12",
"offerName": "3 courses and a kir royale \u00a322.50"
}, {
"restaurantName": "Clos Maggiore",
"restaurantCode": "clos-maggiore",
"address": "33 King Street",
"town": "Covent Garden",
"county": "London",
"postcode": "WC2E 8JD",
"lat": "51.5116900",
"lng": "-0.1247700",
"largeImage": "1",
"offerTypeId": "12",
"offerName": "2 courses: \u00a315.50"
}, {
"restaurantName": "Navajo Joe",
"restaurantCode": "navajo-joe",
"address": "34 King Street",
"town": "Covent Garden",
"county": "London",
"postcode": "WC2E 8JD",
"lat": "51.5116900",
"lng": "-0.1247700",
"largeImage": "1",
"offerTypeId": "1",
"offerName": "50% Off Your Food "
}, {
"restaurantName": "Le Deuxieme",
"restaurantCode": "le-deuxieme",
"address": "65a Long Acre, Covent Garden",
"town": "London",
"county": "West End London",
"postcode": "WC2E 9JD",
"lat": "51.5139100",
"lng": "-0.1227800",
"largeImage": "1",
"offerTypeId": "12",
"offerName": "Sunday offer: 3 courses &amp;amp; half bottle of wine \u00a320"
}]
</code></pre>
