简体   繁体   中英

Looping through JSON data in jQuery and using correct Array Value

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.

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

Any help gratefully appreciated!

function updateHomepageSlideshow(data)
{
var preloadArr = new Array();
var preloadArrName = new Array();
var preloadArrOffer = new Array();
var i;

/* preload images */
for(i=0; i < 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]);
}
}

The JSON

[{
"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; half bottle of wine \u00a320"
}]

You should use

var currImg = 0; // let's start at first image
var intID = setInterval(changeImg, 6000); // you don't need the data and you were passing the result of the function, not the function
/* image rotator */
function changeImg(data){
    $('#amazingOffersAt').hide();
    var index = currImg++%preloadArrName.length;
    $('#homepageRestaurantNameId').html(preloadArrName[index]);
    $('#homepageRotatingImage').css('background-image','url(' + preloadArr[index].src +')');
    $('#homepageRestaurantOfferId').html(preloadArrOffer[index]);
}

Your code wasn't using the same index at each line as currImg++ increments currImg .

To be more precise, currImg++%preloadArrayName.length is (currImg++)%preloadArrayName.length (see precedence ) : it takes the modulo of currImg to ensure to get a value in [0, preloadArrayName.length[ (ie a valid index in the array) and increments currImg .

Note that your code will only work if the 3 arrays have the same size. Instead of using 3 arrays I would have used only one array but containing objects (like what you have in your json).

Are you sure about the method you are using to increment the index in changeImg()? What is the point of doing the modulus operation? Maybe I am missing something.

If you want to simply loop through the images, why not just do this:

var currImg = 0;

function changeImg(data){

if(currImg == preloadArrName.length - 1){
   currImg = 0; // reset index to the first element
}else{
   currImg++ // increment the index to the next element
}
$('#amazingOffersAt').hide();

$('#homepageRestaurantNameId').html(preloadArrName[index]);
$('#homepageRotatingImage').css('background-image','url(' + preloadArr[index].src +')');
$('#homepageRestaurantOfferId').html(preloadArrOffer[index]);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM