0

I have a function which loops through 28 images.

The first images are 1234jd01.jpg and then 1234jd02.jpg all the way to 09.jpg

After the loop has gone from 01 to 09 it should go to 10, 12 and all the way up 28 and then loop back.

However my code just does 1,2,4 - 9 and then 10, 11 which works.

Can I add a 0 to the first set if it is a single digit and then stop at 28? So the first block of images is not showing as missing?

jsFiddle is: http://jsfiddle.net/khaleelm/Vv2u3/2/

My code is

$(function () {
    var i = 01;
    var interval = setInterval(function () {
        jQuery('.animationMax img').attr({
            src: 'http://jdsports.scene7.com/is/image/JDSports/127932jd' + i + '?hei=255&wid=427&resmode=sharp&op_usm=1.1,0.5,0,0&defaultImage=JDSports/sizeImageMissing'
        });
        i++;
        if (i === 28) i = 01; //38 images has been shown, stop the interval
    }, 100);
});
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209

2 Answers2

2

Try this..........

Code:

$(function () {
    var i = 1;
    var interval = setInterval(function () {
        jQuery('.animationMax img').attr({
            src: 'http://jdsports.scene7.com/is/image/JDSports/127932jd'+('0'+i).slice(-2)+ '?hei=255&wid=427&resmode=sharp&op_usm=1.1,0.5,0,0&defaultImage=JDSports/sizeImageMissing'
        });
        i++;
        if (i === 28) i = 1; //38 images has been shown, stop the interval
    }, 100); });

UPDATED FIDDLE......................................................................

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • 1
    Also please note this ties into a bigger question if you fancy answering it: http://stackoverflow.com/questions/20745790/how-can-i-call-clearinterval-outside-of-a-function-in-jquery-outside-the-setint?noredirect=1#comment31086400_20745790 – TheBlackBenzKid Dec 23 '13 at 14:46
  • May I request you to please [vote to undelete this question please](https://stackoverflow.com/q/48831106/548225) – anubhava Feb 23 '21 at 04:49
1

Try this:

$(function () {
    var i = "01";
    var interval = setInterval(function () {
        jQuery('.animationMax img').attr({
            src: 'http://jdsports.scene7.com/is/image/JDSports/127932jd' + i + '?hei=255&wid=427&resmode=sharp&op_usm=1.1,0.5,0,0&defaultImage=JDSports/sizeImageMissing'
        });
        i++;
        i = ('0' + i).slice(-2);
        if (i == 28) i = "01"; //38 images has been shown, stop the interval
    }, 100);
});

Fiddle example

97ldave
  • 5,249
  • 4
  • 25
  • 39
  • Thanks. I added some rep for you. Also please note this ties into a bigger question if you fancy answering it: http://stackoverflow.com/questions/20745790/how-can-i-call-clearinterval-outside-of-a-function-in-jquery-outside-the-setint?noredirect=1#comment31086400_20745790 – TheBlackBenzKid Dec 23 '13 at 14:46