I'm missing some little thing.. prints the array but doesn't wait in between lines.
<script type="text/javascript">
function showLines()
{
arr =
[
"Hi!",
"Welcome!",
"Hello!"
]
var duration=2000;
document.getElementById("quotes").innerHTML=arr;
setTimeout('showLines()',duration);
}
</script>
Most answers here are re initializing your array on each iteration.It makes no sense to do that. You should do it like this:
<script type="text/javascript">
function showLines(){
var arr =
[
"Hi!",
"Welcome!",
"Hello!"
], i = 0;
(function showLinesHelper(){
document.getElementById("quotes").innerHTML += arr[i++]+'<br />';
if(i < arr.length)
setTimeout(showLinesHelper, 2000);
})();
}
</script>
This way it works, and your array, and i are only initialized once.
EDIT In response to comment:
<script type="text/javascript">
function showLines(){
var arr =
[["Hi!", 3000],
["Welcome!", 500],
["Hello!", 1000]]
, i = 0;
function showLinesHelper(){
document.getElementById("quotes").innerHTML += arr[i++][0]+'<br />';
if(i < arr.length)
setTimeout(showLinesHelper, arr[i][1]);
}
setTimeout(showLinesHelper, arr[0][1]);
}
</script>