As you want the first line to be displayed immediately, you need to wrap the code in a function so that you can make the initial call.
, ( ).
:
function processLine(eachLine, count)
{
if (eachLine.length){
doP(eachLine[0], count);
setTimeout(function(){
processLine(eachLine.slice(1), count+1);
},10000);
}
}
processLine(eachLine,1);
:
.
var eachLine = ["Hi", "there", "I", "am", "lines", "of", "text"];
function doP(line, count){
$('body').append(count + ": " + line + "<br/>");
}
function processLine(eachLine, count)
{
if (eachLine.length){
doP(eachLine[0], count)
setTimeout(function(){
processLine(eachLine.slice(1), count+1);
},1000);
}
}
processLine(eachLine,1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Hide resultsetTimeout()
:
var i = 0;
function processLine(){
if(eachLine[i++].length > 0){
doP(eachLine[i], i);
}
if(i < eachLine.length) {
setTimeout(processLine, 10000);
}
}
processLine();
var eachLine = ["Hi", "there", "I", "am", "lines", "of", "text"];
var i = 0, len = eachLine.length;
var i = 0;
function processLine(){
if(eachLine[i++].length > 0){
doP(eachLine[i], i);
}
if(i < eachLine.length) {
setTimeout(processLine, 10000);
}
}
processLine();
function doP(line, count){
$('body').append(count + ": " + line + "<br/>");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
Hide result