I have an arrays=[John; Alex; Mark]
, I wanna to show the elements of this array one by one by 3 second delay.
for (var i=0; i<=3; i++)
{
Your code won't work, since you set four timeouts of 2000 milliseconds (i.e. 2 seconds) at a time. You'd better use closure that sets three timeouts (by number of elements in array) with 3000 milliseconds of delay. It can be done with the following code (note that setTimeout
is written from the small letter):
var s = ["John", "Alex", "Mark"];
for (var i = 0; i < s.length; i++) {
(function(i) {
setTimeout(function() {
x.innerHTML = s[i];
}, 3000 * i);
})(i);
}
DEMO: http://jsfiddle.net/6Ne6z/