Making an image slideshow

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite
1












I wrote a function to make a image slideshow in javascript using for() loop, setTimeout() and setInterval(), but I have doubts that is this a right approach to use a for() loop and setTimeout() together?



Is there a better way to do it...?






var myImage = document.querySelectorAll(".banner");
var arrayImage = ["https://via.placeholder.com/150x150/f00/fff?text=Slider1", "https://via.placeholder.com/150x150/f0f/fff?text=Slider2", "https://via.placeholder.com/150x150/00f/fff?text=Slider3"];
var imageIndex = 0;

function slideChange()
for (var i = 0; i < arrayImage.length; i++)
(function(i)
setTimeout(function()
myImage[i].setAttribute("src", "");
, 2000 * (i + 1));
setTimeout(function()
myImage[i].setAttribute("src", arrayImage[i]);
, 2000 * i);
)(i)


slideChange();
setInterval(function()
slideChange();
, (2000 * arrayImage.length))

.banner 
display: block;

<img class="banner">
<img class="banner">
<img class="banner">









share|improve this question





















  • Based on your demo, it doesn't appear that this works correctly as a slideshow. The timing is off — there is sometimes more or fewer than one visible image. Before proceeding with a review, you'll either have to fix your code, or change the description of what the intended behavior is.
    – 200_success
    Mar 1 at 6:02











  • @200_success sorry to say but I think a slideshow is something like the slide changing after some time....is it not...?
    – Bhuwan
    Mar 1 at 6:28










  • Yes, but the timing of the appearances / disappearances becomes desynchronized after a while. I've reopened the question so that that issue may be addressed in an answer.
    – 200_success
    Mar 1 at 6:31










  • works fine for me in firefox
    – Occam's Razor
    Mar 2 at 13:24
















up vote
0
down vote

favorite
1












I wrote a function to make a image slideshow in javascript using for() loop, setTimeout() and setInterval(), but I have doubts that is this a right approach to use a for() loop and setTimeout() together?



Is there a better way to do it...?






var myImage = document.querySelectorAll(".banner");
var arrayImage = ["https://via.placeholder.com/150x150/f00/fff?text=Slider1", "https://via.placeholder.com/150x150/f0f/fff?text=Slider2", "https://via.placeholder.com/150x150/00f/fff?text=Slider3"];
var imageIndex = 0;

function slideChange()
for (var i = 0; i < arrayImage.length; i++)
(function(i)
setTimeout(function()
myImage[i].setAttribute("src", "");
, 2000 * (i + 1));
setTimeout(function()
myImage[i].setAttribute("src", arrayImage[i]);
, 2000 * i);
)(i)


slideChange();
setInterval(function()
slideChange();
, (2000 * arrayImage.length))

.banner 
display: block;

<img class="banner">
<img class="banner">
<img class="banner">









share|improve this question





















  • Based on your demo, it doesn't appear that this works correctly as a slideshow. The timing is off — there is sometimes more or fewer than one visible image. Before proceeding with a review, you'll either have to fix your code, or change the description of what the intended behavior is.
    – 200_success
    Mar 1 at 6:02











  • @200_success sorry to say but I think a slideshow is something like the slide changing after some time....is it not...?
    – Bhuwan
    Mar 1 at 6:28










  • Yes, but the timing of the appearances / disappearances becomes desynchronized after a while. I've reopened the question so that that issue may be addressed in an answer.
    – 200_success
    Mar 1 at 6:31










  • works fine for me in firefox
    – Occam's Razor
    Mar 2 at 13:24












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I wrote a function to make a image slideshow in javascript using for() loop, setTimeout() and setInterval(), but I have doubts that is this a right approach to use a for() loop and setTimeout() together?



Is there a better way to do it...?






var myImage = document.querySelectorAll(".banner");
var arrayImage = ["https://via.placeholder.com/150x150/f00/fff?text=Slider1", "https://via.placeholder.com/150x150/f0f/fff?text=Slider2", "https://via.placeholder.com/150x150/00f/fff?text=Slider3"];
var imageIndex = 0;

function slideChange()
for (var i = 0; i < arrayImage.length; i++)
(function(i)
setTimeout(function()
myImage[i].setAttribute("src", "");
, 2000 * (i + 1));
setTimeout(function()
myImage[i].setAttribute("src", arrayImage[i]);
, 2000 * i);
)(i)


slideChange();
setInterval(function()
slideChange();
, (2000 * arrayImage.length))

.banner 
display: block;

<img class="banner">
<img class="banner">
<img class="banner">









share|improve this question













I wrote a function to make a image slideshow in javascript using for() loop, setTimeout() and setInterval(), but I have doubts that is this a right approach to use a for() loop and setTimeout() together?



Is there a better way to do it...?






var myImage = document.querySelectorAll(".banner");
var arrayImage = ["https://via.placeholder.com/150x150/f00/fff?text=Slider1", "https://via.placeholder.com/150x150/f0f/fff?text=Slider2", "https://via.placeholder.com/150x150/00f/fff?text=Slider3"];
var imageIndex = 0;

function slideChange()
for (var i = 0; i < arrayImage.length; i++)
(function(i)
setTimeout(function()
myImage[i].setAttribute("src", "");
, 2000 * (i + 1));
setTimeout(function()
myImage[i].setAttribute("src", arrayImage[i]);
, 2000 * i);
)(i)


slideChange();
setInterval(function()
slideChange();
, (2000 * arrayImage.length))

.banner 
display: block;

<img class="banner">
<img class="banner">
<img class="banner">








var myImage = document.querySelectorAll(".banner");
var arrayImage = ["https://via.placeholder.com/150x150/f00/fff?text=Slider1", "https://via.placeholder.com/150x150/f0f/fff?text=Slider2", "https://via.placeholder.com/150x150/00f/fff?text=Slider3"];
var imageIndex = 0;

function slideChange()
for (var i = 0; i < arrayImage.length; i++)
(function(i)
setTimeout(function()
myImage[i].setAttribute("src", "");
, 2000 * (i + 1));
setTimeout(function()
myImage[i].setAttribute("src", arrayImage[i]);
, 2000 * i);
)(i)


slideChange();
setInterval(function()
slideChange();
, (2000 * arrayImage.length))

.banner 
display: block;

<img class="banner">
<img class="banner">
<img class="banner">





var myImage = document.querySelectorAll(".banner");
var arrayImage = ["https://via.placeholder.com/150x150/f00/fff?text=Slider1", "https://via.placeholder.com/150x150/f0f/fff?text=Slider2", "https://via.placeholder.com/150x150/00f/fff?text=Slider3"];
var imageIndex = 0;

function slideChange()
for (var i = 0; i < arrayImage.length; i++)
(function(i)
setTimeout(function()
myImage[i].setAttribute("src", "");
, 2000 * (i + 1));
setTimeout(function()
myImage[i].setAttribute("src", arrayImage[i]);
, 2000 * i);
)(i)


slideChange();
setInterval(function()
slideChange();
, (2000 * arrayImage.length))

.banner 
display: block;

<img class="banner">
<img class="banner">
<img class="banner">








share|improve this question












share|improve this question




share|improve this question








edited Mar 1 at 6:31









200_success

123k14142399




123k14142399









asked Mar 1 at 5:34









Bhuwan

1012




1012











  • Based on your demo, it doesn't appear that this works correctly as a slideshow. The timing is off — there is sometimes more or fewer than one visible image. Before proceeding with a review, you'll either have to fix your code, or change the description of what the intended behavior is.
    – 200_success
    Mar 1 at 6:02











  • @200_success sorry to say but I think a slideshow is something like the slide changing after some time....is it not...?
    – Bhuwan
    Mar 1 at 6:28










  • Yes, but the timing of the appearances / disappearances becomes desynchronized after a while. I've reopened the question so that that issue may be addressed in an answer.
    – 200_success
    Mar 1 at 6:31










  • works fine for me in firefox
    – Occam's Razor
    Mar 2 at 13:24
















  • Based on your demo, it doesn't appear that this works correctly as a slideshow. The timing is off — there is sometimes more or fewer than one visible image. Before proceeding with a review, you'll either have to fix your code, or change the description of what the intended behavior is.
    – 200_success
    Mar 1 at 6:02











  • @200_success sorry to say but I think a slideshow is something like the slide changing after some time....is it not...?
    – Bhuwan
    Mar 1 at 6:28










  • Yes, but the timing of the appearances / disappearances becomes desynchronized after a while. I've reopened the question so that that issue may be addressed in an answer.
    – 200_success
    Mar 1 at 6:31










  • works fine for me in firefox
    – Occam's Razor
    Mar 2 at 13:24















Based on your demo, it doesn't appear that this works correctly as a slideshow. The timing is off — there is sometimes more or fewer than one visible image. Before proceeding with a review, you'll either have to fix your code, or change the description of what the intended behavior is.
– 200_success
Mar 1 at 6:02





Based on your demo, it doesn't appear that this works correctly as a slideshow. The timing is off — there is sometimes more or fewer than one visible image. Before proceeding with a review, you'll either have to fix your code, or change the description of what the intended behavior is.
– 200_success
Mar 1 at 6:02













@200_success sorry to say but I think a slideshow is something like the slide changing after some time....is it not...?
– Bhuwan
Mar 1 at 6:28




@200_success sorry to say but I think a slideshow is something like the slide changing after some time....is it not...?
– Bhuwan
Mar 1 at 6:28












Yes, but the timing of the appearances / disappearances becomes desynchronized after a while. I've reopened the question so that that issue may be addressed in an answer.
– 200_success
Mar 1 at 6:31




Yes, but the timing of the appearances / disappearances becomes desynchronized after a while. I've reopened the question so that that issue may be addressed in an answer.
– 200_success
Mar 1 at 6:31












works fine for me in firefox
– Occam's Razor
Mar 2 at 13:24




works fine for me in firefox
– Occam's Razor
Mar 2 at 13:24










1 Answer
1






active

oldest

votes

















up vote
1
down vote













There's really no purpose in having 3 banner images if you're going to add and remove the image source from each one. You might as well just have a single image and change the source of it on each iteration.



When you do that trick with setTimeout, where your timeout is the index of the array multiplied by an offset, you are effectively creating a new thread for every iteration, which can be memory intensive when you have a lot of images. A better approach would be to use recursion.



Consider this slowLoop function.



/**
* Execute the loopBody function once for each item in the items array,
* waiting for the done function (which is passed into the loopBody function)
* to be called before proceeding to the next item in the array.
* @param Array items - The array of items to iterate through
* @param Function loopBody - A function to execute on each item in the array.
* This function is passed 3 arguments -
* 1. The item in the current iteration,
* 2. The index of the item in the array,
* 3. A function to be called when the iteration may continue.
* @returns Promise - A promise that is resolved when all the items in the
* in the array have been iterated through.
*/
function slowLoop(items, loopBody)
return new Promise(f => 0;
let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
loopBody(items[idx], idx, cb);
);



Using this you're able to keep the entire script on a single thread.






var myImage = document.querySelector("#banner");

var arrayImage = [
"https://via.placeholder.com/150x150/f00/fff?text=Slider1",
"https://via.placeholder.com/150x150/f0f/fff?text=Slider2",
"https://via.placeholder.com/150x150/00f/fff?text=Slider3"
];

function slowLoop(items, loopBody)
return new Promise(f => 0;
let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
loopBody(items[idx], idx, cb);
);


function slideChange()
slowLoop(arrayImage, function(img, idx, done)
myImage.setAttribute("src", img);
setTimeout(done, 2000);
).then(slideChange);


slideChange();

.banner 
display: block;

<img id="banner">








share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188583%2fmaking-an-image-slideshow%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    There's really no purpose in having 3 banner images if you're going to add and remove the image source from each one. You might as well just have a single image and change the source of it on each iteration.



    When you do that trick with setTimeout, where your timeout is the index of the array multiplied by an offset, you are effectively creating a new thread for every iteration, which can be memory intensive when you have a lot of images. A better approach would be to use recursion.



    Consider this slowLoop function.



    /**
    * Execute the loopBody function once for each item in the items array,
    * waiting for the done function (which is passed into the loopBody function)
    * to be called before proceeding to the next item in the array.
    * @param Array items - The array of items to iterate through
    * @param Function loopBody - A function to execute on each item in the array.
    * This function is passed 3 arguments -
    * 1. The item in the current iteration,
    * 2. The index of the item in the array,
    * 3. A function to be called when the iteration may continue.
    * @returns Promise - A promise that is resolved when all the items in the
    * in the array have been iterated through.
    */
    function slowLoop(items, loopBody)
    return new Promise(f => 0;
    let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
    loopBody(items[idx], idx, cb);
    );



    Using this you're able to keep the entire script on a single thread.






    var myImage = document.querySelector("#banner");

    var arrayImage = [
    "https://via.placeholder.com/150x150/f00/fff?text=Slider1",
    "https://via.placeholder.com/150x150/f0f/fff?text=Slider2",
    "https://via.placeholder.com/150x150/00f/fff?text=Slider3"
    ];

    function slowLoop(items, loopBody)
    return new Promise(f => 0;
    let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
    loopBody(items[idx], idx, cb);
    );


    function slideChange()
    slowLoop(arrayImage, function(img, idx, done)
    myImage.setAttribute("src", img);
    setTimeout(done, 2000);
    ).then(slideChange);


    slideChange();

    .banner 
    display: block;

    <img id="banner">








    share|improve this answer



























      up vote
      1
      down vote













      There's really no purpose in having 3 banner images if you're going to add and remove the image source from each one. You might as well just have a single image and change the source of it on each iteration.



      When you do that trick with setTimeout, where your timeout is the index of the array multiplied by an offset, you are effectively creating a new thread for every iteration, which can be memory intensive when you have a lot of images. A better approach would be to use recursion.



      Consider this slowLoop function.



      /**
      * Execute the loopBody function once for each item in the items array,
      * waiting for the done function (which is passed into the loopBody function)
      * to be called before proceeding to the next item in the array.
      * @param Array items - The array of items to iterate through
      * @param Function loopBody - A function to execute on each item in the array.
      * This function is passed 3 arguments -
      * 1. The item in the current iteration,
      * 2. The index of the item in the array,
      * 3. A function to be called when the iteration may continue.
      * @returns Promise - A promise that is resolved when all the items in the
      * in the array have been iterated through.
      */
      function slowLoop(items, loopBody)
      return new Promise(f => 0;
      let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
      loopBody(items[idx], idx, cb);
      );



      Using this you're able to keep the entire script on a single thread.






      var myImage = document.querySelector("#banner");

      var arrayImage = [
      "https://via.placeholder.com/150x150/f00/fff?text=Slider1",
      "https://via.placeholder.com/150x150/f0f/fff?text=Slider2",
      "https://via.placeholder.com/150x150/00f/fff?text=Slider3"
      ];

      function slowLoop(items, loopBody)
      return new Promise(f => 0;
      let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
      loopBody(items[idx], idx, cb);
      );


      function slideChange()
      slowLoop(arrayImage, function(img, idx, done)
      myImage.setAttribute("src", img);
      setTimeout(done, 2000);
      ).then(slideChange);


      slideChange();

      .banner 
      display: block;

      <img id="banner">








      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        There's really no purpose in having 3 banner images if you're going to add and remove the image source from each one. You might as well just have a single image and change the source of it on each iteration.



        When you do that trick with setTimeout, where your timeout is the index of the array multiplied by an offset, you are effectively creating a new thread for every iteration, which can be memory intensive when you have a lot of images. A better approach would be to use recursion.



        Consider this slowLoop function.



        /**
        * Execute the loopBody function once for each item in the items array,
        * waiting for the done function (which is passed into the loopBody function)
        * to be called before proceeding to the next item in the array.
        * @param Array items - The array of items to iterate through
        * @param Function loopBody - A function to execute on each item in the array.
        * This function is passed 3 arguments -
        * 1. The item in the current iteration,
        * 2. The index of the item in the array,
        * 3. A function to be called when the iteration may continue.
        * @returns Promise - A promise that is resolved when all the items in the
        * in the array have been iterated through.
        */
        function slowLoop(items, loopBody)
        return new Promise(f => 0;
        let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
        loopBody(items[idx], idx, cb);
        );



        Using this you're able to keep the entire script on a single thread.






        var myImage = document.querySelector("#banner");

        var arrayImage = [
        "https://via.placeholder.com/150x150/f00/fff?text=Slider1",
        "https://via.placeholder.com/150x150/f0f/fff?text=Slider2",
        "https://via.placeholder.com/150x150/00f/fff?text=Slider3"
        ];

        function slowLoop(items, loopBody)
        return new Promise(f => 0;
        let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
        loopBody(items[idx], idx, cb);
        );


        function slideChange()
        slowLoop(arrayImage, function(img, idx, done)
        myImage.setAttribute("src", img);
        setTimeout(done, 2000);
        ).then(slideChange);


        slideChange();

        .banner 
        display: block;

        <img id="banner">








        share|improve this answer















        There's really no purpose in having 3 banner images if you're going to add and remove the image source from each one. You might as well just have a single image and change the source of it on each iteration.



        When you do that trick with setTimeout, where your timeout is the index of the array multiplied by an offset, you are effectively creating a new thread for every iteration, which can be memory intensive when you have a lot of images. A better approach would be to use recursion.



        Consider this slowLoop function.



        /**
        * Execute the loopBody function once for each item in the items array,
        * waiting for the done function (which is passed into the loopBody function)
        * to be called before proceeding to the next item in the array.
        * @param Array items - The array of items to iterate through
        * @param Function loopBody - A function to execute on each item in the array.
        * This function is passed 3 arguments -
        * 1. The item in the current iteration,
        * 2. The index of the item in the array,
        * 3. A function to be called when the iteration may continue.
        * @returns Promise - A promise that is resolved when all the items in the
        * in the array have been iterated through.
        */
        function slowLoop(items, loopBody)
        return new Promise(f => 0;
        let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
        loopBody(items[idx], idx, cb);
        );



        Using this you're able to keep the entire script on a single thread.






        var myImage = document.querySelector("#banner");

        var arrayImage = [
        "https://via.placeholder.com/150x150/f00/fff?text=Slider1",
        "https://via.placeholder.com/150x150/f0f/fff?text=Slider2",
        "https://via.placeholder.com/150x150/00f/fff?text=Slider3"
        ];

        function slowLoop(items, loopBody)
        return new Promise(f => 0;
        let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
        loopBody(items[idx], idx, cb);
        );


        function slideChange()
        slowLoop(arrayImage, function(img, idx, done)
        myImage.setAttribute("src", img);
        setTimeout(done, 2000);
        ).then(slideChange);


        slideChange();

        .banner 
        display: block;

        <img id="banner">








        var myImage = document.querySelector("#banner");

        var arrayImage = [
        "https://via.placeholder.com/150x150/f00/fff?text=Slider1",
        "https://via.placeholder.com/150x150/f0f/fff?text=Slider2",
        "https://via.placeholder.com/150x150/00f/fff?text=Slider3"
        ];

        function slowLoop(items, loopBody)
        return new Promise(f => 0;
        let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
        loopBody(items[idx], idx, cb);
        );


        function slideChange()
        slowLoop(arrayImage, function(img, idx, done)
        myImage.setAttribute("src", img);
        setTimeout(done, 2000);
        ).then(slideChange);


        slideChange();

        .banner 
        display: block;

        <img id="banner">





        var myImage = document.querySelector("#banner");

        var arrayImage = [
        "https://via.placeholder.com/150x150/f00/fff?text=Slider1",
        "https://via.placeholder.com/150x150/f0f/fff?text=Slider2",
        "https://via.placeholder.com/150x150/00f/fff?text=Slider3"
        ];

        function slowLoop(items, loopBody)
        return new Promise(f => 0;
        let cb = items[idx + 1] ? () => slowLoop(items, loopBody, done, idx + 1) : done;
        loopBody(items[idx], idx, cb);
        );


        function slideChange()
        slowLoop(arrayImage, function(img, idx, done)
        myImage.setAttribute("src", img);
        setTimeout(done, 2000);
        ).then(slideChange);


        slideChange();

        .banner 
        display: block;

        <img id="banner">






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Mar 2 at 14:05


























        answered Mar 2 at 13:59









        Occam's Razor

        1,982513




        1,982513






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f188583%2fmaking-an-image-slideshow%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation