Making an image slideshow
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
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">
javascript image animation
add a comment |Â
up vote
0
down vote
favorite
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">
javascript image animation
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
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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">
javascript image animation
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">
javascript image animation
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
add a comment |Â
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
add a comment |Â
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">
add a comment |Â
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">
add a comment |Â
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">
add a comment |Â
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">
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">
edited Mar 2 at 14:05
answered Mar 2 at 13:59
Occam's Razor
1,982513
1,982513
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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