0

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"
}]

2 Answers 2

2

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[ (i.e. 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).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I tried that, however, although it showed the first slide, it didn't show any more... It however was the correct image and text on it!
Many thanks, that solved it! And thanks for the explination, makes a lot more sense now... Out of interest, what format would I use if I was to use one array with containing objects?
0

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]);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.