For loop pauses, but prints out the Array all at once
I'm having trouble Grok'ing a simple JavaScript for loop.
Here is an example:
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 1; i <= 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
for(const value of arr) {
document.write(value);
document.write("<br />");
}
}, i * 1000);
}
What I'm trying to do is pause after it prints the first value, but instead it runs the entire For loop then pauses. I think I'm trying to do something with for loops that just won't work, but I would like to know why.
Any help would be appreciated.
Plunkr here: https://plnkr.co/edit/tnmFrIRTDJI8T294Qh4z?p=preview
The example Javascript, setTimeout loops? didn't help me figure it out. I still got the concept wrong as George Pantazes points out.
javascript
add a comment |
I'm having trouble Grok'ing a simple JavaScript for loop.
Here is an example:
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 1; i <= 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
for(const value of arr) {
document.write(value);
document.write("<br />");
}
}, i * 1000);
}
What I'm trying to do is pause after it prints the first value, but instead it runs the entire For loop then pauses. I think I'm trying to do something with for loops that just won't work, but I would like to know why.
Any help would be appreciated.
Plunkr here: https://plnkr.co/edit/tnmFrIRTDJI8T294Qh4z?p=preview
The example Javascript, setTimeout loops? didn't help me figure it out. I still got the concept wrong as George Pantazes points out.
javascript
Possible duplicate of Javascript, setTimeout loops?
– Tyler Roper
Nov 13 '18 at 19:05
2
By running your code, you can see that you are printing the entire array within thesetTimeout
. Instead of looping through the entirearr
in thesetTimeout
for
loop, if you access your array likearr[i]
and only print one item instead of all of them, your code would work as you intend.
– George Pantazes
Nov 13 '18 at 19:06
add a comment |
I'm having trouble Grok'ing a simple JavaScript for loop.
Here is an example:
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 1; i <= 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
for(const value of arr) {
document.write(value);
document.write("<br />");
}
}, i * 1000);
}
What I'm trying to do is pause after it prints the first value, but instead it runs the entire For loop then pauses. I think I'm trying to do something with for loops that just won't work, but I would like to know why.
Any help would be appreciated.
Plunkr here: https://plnkr.co/edit/tnmFrIRTDJI8T294Qh4z?p=preview
The example Javascript, setTimeout loops? didn't help me figure it out. I still got the concept wrong as George Pantazes points out.
javascript
I'm having trouble Grok'ing a simple JavaScript for loop.
Here is an example:
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 1; i <= 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
for(const value of arr) {
document.write(value);
document.write("<br />");
}
}, i * 1000);
}
What I'm trying to do is pause after it prints the first value, but instead it runs the entire For loop then pauses. I think I'm trying to do something with for loops that just won't work, but I would like to know why.
Any help would be appreciated.
Plunkr here: https://plnkr.co/edit/tnmFrIRTDJI8T294Qh4z?p=preview
The example Javascript, setTimeout loops? didn't help me figure it out. I still got the concept wrong as George Pantazes points out.
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 1; i <= 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
for(const value of arr) {
document.write(value);
document.write("<br />");
}
}, i * 1000);
}
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 1; i <= 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
for(const value of arr) {
document.write(value);
document.write("<br />");
}
}, i * 1000);
}
javascript
javascript
edited Nov 13 '18 at 19:59
Rostasan
asked Nov 13 '18 at 19:03
RostasanRostasan
105
105
Possible duplicate of Javascript, setTimeout loops?
– Tyler Roper
Nov 13 '18 at 19:05
2
By running your code, you can see that you are printing the entire array within thesetTimeout
. Instead of looping through the entirearr
in thesetTimeout
for
loop, if you access your array likearr[i]
and only print one item instead of all of them, your code would work as you intend.
– George Pantazes
Nov 13 '18 at 19:06
add a comment |
Possible duplicate of Javascript, setTimeout loops?
– Tyler Roper
Nov 13 '18 at 19:05
2
By running your code, you can see that you are printing the entire array within thesetTimeout
. Instead of looping through the entirearr
in thesetTimeout
for
loop, if you access your array likearr[i]
and only print one item instead of all of them, your code would work as you intend.
– George Pantazes
Nov 13 '18 at 19:06
Possible duplicate of Javascript, setTimeout loops?
– Tyler Roper
Nov 13 '18 at 19:05
Possible duplicate of Javascript, setTimeout loops?
– Tyler Roper
Nov 13 '18 at 19:05
2
2
By running your code, you can see that you are printing the entire array within the
setTimeout
. Instead of looping through the entire arr
in the setTimeout
for
loop, if you access your array like arr[i]
and only print one item instead of all of them, your code would work as you intend.– George Pantazes
Nov 13 '18 at 19:06
By running your code, you can see that you are printing the entire array within the
setTimeout
. Instead of looping through the entire arr
in the setTimeout
for
loop, if you access your array like arr[i]
and only print one item instead of all of them, your code would work as you intend.– George Pantazes
Nov 13 '18 at 19:06
add a comment |
3 Answers
3
active
oldest
votes
Inside setTimeout - instead of printing entire array just print current element.
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (let i = 1; i < 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
console.log(arr[i-1])
}, i * 1000);
}
Why are you using 1-i? Is it better to start with i = 1? I do understand that the array index starts at 0. Oh wait, so the array is four long, so you changed the i to equal 1 match the offset? Would it be better if I did i < arr.length? -Thanks
– Rostasan
Nov 13 '18 at 20:04
Yes, you are correct because you have 4 array elements and array index starts from 0
– Nitish Narang
Nov 13 '18 at 20:07
Its better to do i< arr.length. Also, its always preferable to start i with 0 rather than 1
– Nitish Narang
Nov 14 '18 at 6:45
add a comment |
There were multiple issues within your code:
- Be sure to mind that Javascript arrays are 0-based. You were starting at 1.
- Similarly, be sure to mind the ending bound of the index (in your for loop). It was going too far even for a 1-base (it was going to 5 out of 4 available items)
- Within the
setTimeout
, you were printing the entire array by usingfor(const value of arr)
. You were probably trying to pass ini
as an index to index only one element.
With those comments in mind, here is the working code with those parts changed:
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 0; i < arr.length; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
document.write(arr[i]);
document.write("<br />");
}, i * 1000);
}
Refer to @Nitish-Narang's answer for better Javascript code style, although I think my advice about the indices is worth taking as well.
– George Pantazes
Nov 13 '18 at 19:15
wow thanks. I tried to do that by setting i = to the array (out of desperation), but this makes a lot more sense. I have access to the array and i. So that would let me use arr[i].
– Rostasan
Nov 13 '18 at 20:01
add a comment |
An alternative approach to creating a delay by using setTimeout
in a loop is the setInterval
function. setInterval
will execute the function argument each time a specified duration of of milliseconds passes:
var arr = ["Banana", "Orange", "Apple", "Mango"];
var i = 0;
var intervalId = setInterval(logNext, 1000);
function logNext() {
if (i < arr.length) {
console.log(arr[i++]);
} else {
console.log('End of array reached!');
clearInterval(intervalId);
}
}
add a comment |
Your Answer
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: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287874%2ffor-loop-pauses-but-prints-out-the-array-all-at-once%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Inside setTimeout - instead of printing entire array just print current element.
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (let i = 1; i < 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
console.log(arr[i-1])
}, i * 1000);
}
Why are you using 1-i? Is it better to start with i = 1? I do understand that the array index starts at 0. Oh wait, so the array is four long, so you changed the i to equal 1 match the offset? Would it be better if I did i < arr.length? -Thanks
– Rostasan
Nov 13 '18 at 20:04
Yes, you are correct because you have 4 array elements and array index starts from 0
– Nitish Narang
Nov 13 '18 at 20:07
Its better to do i< arr.length. Also, its always preferable to start i with 0 rather than 1
– Nitish Narang
Nov 14 '18 at 6:45
add a comment |
Inside setTimeout - instead of printing entire array just print current element.
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (let i = 1; i < 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
console.log(arr[i-1])
}, i * 1000);
}
Why are you using 1-i? Is it better to start with i = 1? I do understand that the array index starts at 0. Oh wait, so the array is four long, so you changed the i to equal 1 match the offset? Would it be better if I did i < arr.length? -Thanks
– Rostasan
Nov 13 '18 at 20:04
Yes, you are correct because you have 4 array elements and array index starts from 0
– Nitish Narang
Nov 13 '18 at 20:07
Its better to do i< arr.length. Also, its always preferable to start i with 0 rather than 1
– Nitish Narang
Nov 14 '18 at 6:45
add a comment |
Inside setTimeout - instead of printing entire array just print current element.
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (let i = 1; i < 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
console.log(arr[i-1])
}, i * 1000);
}
Inside setTimeout - instead of printing entire array just print current element.
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (let i = 1; i < 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
console.log(arr[i-1])
}, i * 1000);
}
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (let i = 1; i < 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
console.log(arr[i-1])
}, i * 1000);
}
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (let i = 1; i < 5; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
console.log(arr[i-1])
}, i * 1000);
}
answered Nov 13 '18 at 19:09
Nitish NarangNitish Narang
2,9401815
2,9401815
Why are you using 1-i? Is it better to start with i = 1? I do understand that the array index starts at 0. Oh wait, so the array is four long, so you changed the i to equal 1 match the offset? Would it be better if I did i < arr.length? -Thanks
– Rostasan
Nov 13 '18 at 20:04
Yes, you are correct because you have 4 array elements and array index starts from 0
– Nitish Narang
Nov 13 '18 at 20:07
Its better to do i< arr.length. Also, its always preferable to start i with 0 rather than 1
– Nitish Narang
Nov 14 '18 at 6:45
add a comment |
Why are you using 1-i? Is it better to start with i = 1? I do understand that the array index starts at 0. Oh wait, so the array is four long, so you changed the i to equal 1 match the offset? Would it be better if I did i < arr.length? -Thanks
– Rostasan
Nov 13 '18 at 20:04
Yes, you are correct because you have 4 array elements and array index starts from 0
– Nitish Narang
Nov 13 '18 at 20:07
Its better to do i< arr.length. Also, its always preferable to start i with 0 rather than 1
– Nitish Narang
Nov 14 '18 at 6:45
Why are you using 1-i? Is it better to start with i = 1? I do understand that the array index starts at 0. Oh wait, so the array is four long, so you changed the i to equal 1 match the offset? Would it be better if I did i < arr.length? -Thanks
– Rostasan
Nov 13 '18 at 20:04
Why are you using 1-i? Is it better to start with i = 1? I do understand that the array index starts at 0. Oh wait, so the array is four long, so you changed the i to equal 1 match the offset? Would it be better if I did i < arr.length? -Thanks
– Rostasan
Nov 13 '18 at 20:04
Yes, you are correct because you have 4 array elements and array index starts from 0
– Nitish Narang
Nov 13 '18 at 20:07
Yes, you are correct because you have 4 array elements and array index starts from 0
– Nitish Narang
Nov 13 '18 at 20:07
Its better to do i< arr.length. Also, its always preferable to start i with 0 rather than 1
– Nitish Narang
Nov 14 '18 at 6:45
Its better to do i< arr.length. Also, its always preferable to start i with 0 rather than 1
– Nitish Narang
Nov 14 '18 at 6:45
add a comment |
There were multiple issues within your code:
- Be sure to mind that Javascript arrays are 0-based. You were starting at 1.
- Similarly, be sure to mind the ending bound of the index (in your for loop). It was going too far even for a 1-base (it was going to 5 out of 4 available items)
- Within the
setTimeout
, you were printing the entire array by usingfor(const value of arr)
. You were probably trying to pass ini
as an index to index only one element.
With those comments in mind, here is the working code with those parts changed:
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 0; i < arr.length; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
document.write(arr[i]);
document.write("<br />");
}, i * 1000);
}
Refer to @Nitish-Narang's answer for better Javascript code style, although I think my advice about the indices is worth taking as well.
– George Pantazes
Nov 13 '18 at 19:15
wow thanks. I tried to do that by setting i = to the array (out of desperation), but this makes a lot more sense. I have access to the array and i. So that would let me use arr[i].
– Rostasan
Nov 13 '18 at 20:01
add a comment |
There were multiple issues within your code:
- Be sure to mind that Javascript arrays are 0-based. You were starting at 1.
- Similarly, be sure to mind the ending bound of the index (in your for loop). It was going too far even for a 1-base (it was going to 5 out of 4 available items)
- Within the
setTimeout
, you were printing the entire array by usingfor(const value of arr)
. You were probably trying to pass ini
as an index to index only one element.
With those comments in mind, here is the working code with those parts changed:
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 0; i < arr.length; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
document.write(arr[i]);
document.write("<br />");
}, i * 1000);
}
Refer to @Nitish-Narang's answer for better Javascript code style, although I think my advice about the indices is worth taking as well.
– George Pantazes
Nov 13 '18 at 19:15
wow thanks. I tried to do that by setting i = to the array (out of desperation), but this makes a lot more sense. I have access to the array and i. So that would let me use arr[i].
– Rostasan
Nov 13 '18 at 20:01
add a comment |
There were multiple issues within your code:
- Be sure to mind that Javascript arrays are 0-based. You were starting at 1.
- Similarly, be sure to mind the ending bound of the index (in your for loop). It was going too far even for a 1-base (it was going to 5 out of 4 available items)
- Within the
setTimeout
, you were printing the entire array by usingfor(const value of arr)
. You were probably trying to pass ini
as an index to index only one element.
With those comments in mind, here is the working code with those parts changed:
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 0; i < arr.length; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
document.write(arr[i]);
document.write("<br />");
}, i * 1000);
}
There were multiple issues within your code:
- Be sure to mind that Javascript arrays are 0-based. You were starting at 1.
- Similarly, be sure to mind the ending bound of the index (in your for loop). It was going too far even for a 1-base (it was going to 5 out of 4 available items)
- Within the
setTimeout
, you were printing the entire array by usingfor(const value of arr)
. You were probably trying to pass ini
as an index to index only one element.
With those comments in mind, here is the working code with those parts changed:
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 0; i < arr.length; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
document.write(arr[i]);
document.write("<br />");
}, i * 1000);
}
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 0; i < arr.length; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
document.write(arr[i]);
document.write("<br />");
}, i * 1000);
}
var arr = ["Banana", "Orange", "Apple", "Mango"];
for (i = 0; i < arr.length; ++i) {
setDelay(i);
}
function setDelay(i) {
setTimeout(function(){
document.write(arr[i]);
document.write("<br />");
}, i * 1000);
}
answered Nov 13 '18 at 19:13
George PantazesGeorge Pantazes
472617
472617
Refer to @Nitish-Narang's answer for better Javascript code style, although I think my advice about the indices is worth taking as well.
– George Pantazes
Nov 13 '18 at 19:15
wow thanks. I tried to do that by setting i = to the array (out of desperation), but this makes a lot more sense. I have access to the array and i. So that would let me use arr[i].
– Rostasan
Nov 13 '18 at 20:01
add a comment |
Refer to @Nitish-Narang's answer for better Javascript code style, although I think my advice about the indices is worth taking as well.
– George Pantazes
Nov 13 '18 at 19:15
wow thanks. I tried to do that by setting i = to the array (out of desperation), but this makes a lot more sense. I have access to the array and i. So that would let me use arr[i].
– Rostasan
Nov 13 '18 at 20:01
Refer to @Nitish-Narang's answer for better Javascript code style, although I think my advice about the indices is worth taking as well.
– George Pantazes
Nov 13 '18 at 19:15
Refer to @Nitish-Narang's answer for better Javascript code style, although I think my advice about the indices is worth taking as well.
– George Pantazes
Nov 13 '18 at 19:15
wow thanks. I tried to do that by setting i = to the array (out of desperation), but this makes a lot more sense. I have access to the array and i. So that would let me use arr[i].
– Rostasan
Nov 13 '18 at 20:01
wow thanks. I tried to do that by setting i = to the array (out of desperation), but this makes a lot more sense. I have access to the array and i. So that would let me use arr[i].
– Rostasan
Nov 13 '18 at 20:01
add a comment |
An alternative approach to creating a delay by using setTimeout
in a loop is the setInterval
function. setInterval
will execute the function argument each time a specified duration of of milliseconds passes:
var arr = ["Banana", "Orange", "Apple", "Mango"];
var i = 0;
var intervalId = setInterval(logNext, 1000);
function logNext() {
if (i < arr.length) {
console.log(arr[i++]);
} else {
console.log('End of array reached!');
clearInterval(intervalId);
}
}
add a comment |
An alternative approach to creating a delay by using setTimeout
in a loop is the setInterval
function. setInterval
will execute the function argument each time a specified duration of of milliseconds passes:
var arr = ["Banana", "Orange", "Apple", "Mango"];
var i = 0;
var intervalId = setInterval(logNext, 1000);
function logNext() {
if (i < arr.length) {
console.log(arr[i++]);
} else {
console.log('End of array reached!');
clearInterval(intervalId);
}
}
add a comment |
An alternative approach to creating a delay by using setTimeout
in a loop is the setInterval
function. setInterval
will execute the function argument each time a specified duration of of milliseconds passes:
var arr = ["Banana", "Orange", "Apple", "Mango"];
var i = 0;
var intervalId = setInterval(logNext, 1000);
function logNext() {
if (i < arr.length) {
console.log(arr[i++]);
} else {
console.log('End of array reached!');
clearInterval(intervalId);
}
}
An alternative approach to creating a delay by using setTimeout
in a loop is the setInterval
function. setInterval
will execute the function argument each time a specified duration of of milliseconds passes:
var arr = ["Banana", "Orange", "Apple", "Mango"];
var i = 0;
var intervalId = setInterval(logNext, 1000);
function logNext() {
if (i < arr.length) {
console.log(arr[i++]);
} else {
console.log('End of array reached!');
clearInterval(intervalId);
}
}
var arr = ["Banana", "Orange", "Apple", "Mango"];
var i = 0;
var intervalId = setInterval(logNext, 1000);
function logNext() {
if (i < arr.length) {
console.log(arr[i++]);
} else {
console.log('End of array reached!');
clearInterval(intervalId);
}
}
var arr = ["Banana", "Orange", "Apple", "Mango"];
var i = 0;
var intervalId = setInterval(logNext, 1000);
function logNext() {
if (i < arr.length) {
console.log(arr[i++]);
} else {
console.log('End of array reached!');
clearInterval(intervalId);
}
}
answered Nov 13 '18 at 19:09
Tom O.Tom O.
2,36311325
2,36311325
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287874%2ffor-loop-pauses-but-prints-out-the-array-all-at-once%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Javascript, setTimeout loops?
– Tyler Roper
Nov 13 '18 at 19:05
2
By running your code, you can see that you are printing the entire array within the
setTimeout
. Instead of looping through the entirearr
in thesetTimeout
for
loop, if you access your array likearr[i]
and only print one item instead of all of them, your code would work as you intend.– George Pantazes
Nov 13 '18 at 19:06