Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:55:37+00:00 2026-06-14T22:55:37+00:00

I have the below code that I have cobbled together. Basically it should take

  • 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"
}]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:55:39+00:00Added an answer on June 14, 2026 at 10:55 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a plot (sample code pasted below) that I am trying to add
i have this code below that loops through a data structure builds up a
I have below code to retrieve some text from a php document: $.get(server/test.php, function(data){
I have code that looks more or less like the code below but it
I have the code below that hides and shows the navigational bar. It is
I have this javascript code below that uses jquery, it is suppoed to be
below i have a code that runs in most of my simple programs ..
I have a piece of code (below) that can get the text of an
Background: I have a WPF UserControl (MainControl - not shown in code below) that
I have the code below on form so that the user submits the form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.