Rick Joe February 2016

run a function inside FOR every 10 seconds

I have this for inside my script:

for(var i = 0, l = eachLine.length; i < l; i++) {
 if(eachLine[i].length>0){
  doP(eachLine[i], +i);
 }
}

the for read lines from a string and call doP function. What happens is it is too fast and cause some speed trouble in my webside depending on text size.

What I want is to call doP function every 10 seconds... in other words, I want for to wait 10 seconds to call doP function again... how can I make it work?

Answers


Rejith R Krishnan February 2016

Using setInterval()

var i = 0, len = eachLine.length;
function looper(){
    if(i == 0)
        interval = setInterval(looper, 10000)
    if(eachLine[i].length > 0)
        doP(eachLine[i], ++i);
    if(i >= len) 
        clearInterval(interval);
}
looper();

var eachLine = ["Hi", "there", "I", "am", "lines", "of", "text"];
var i = 0, len = eachLine.length;
function looper(){
    if(i == 0)
        interval = setInterval(looper, 2000)
    if(eachLine[i].length > 0)
        doP(eachLine[i], ++i);
    if(i >= len) 
        clearInterval(interval);
}
looper();

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>

Using setTimeout()

var i = 0, len = eachLine.length;
function looper(){
    if(eachLine[i].length > 0)
        doP(eachLine[i], ++i);
    if(i < len) 
        setTimeout(looper, 10000);
}
looper();

var eachLine = ["Hi", "there", "I", "am", "lines", "of", "text"];
var i = 0, len = eachLine.length;
function looper(){
    if(eachLine[i].length > 0)
        doP(eachLine[i], ++i);
    if(i < len) 
        setTimeout(looper, 2000);
}
looper();

function doP(line, count){
    $('body').append(count + ": " + line + "<br/>");
}
<script src="https 


Gone Coding February 2016

As you want the first line to display immediately, you need to wrap the code in a function, so that an initial call can be made.

The other example covers counters, so here is another option (using slice on the array with a recursive function).

Commented recursive version:

function processLine(eachLine, count)
{
    // if there are any array entries left...
    if (eachLine.length){

        // Call the worker function the first line in the array
        doP(eachLine[0], count);

        // Wait 10 seconds then call this function recursively
        setTimeout(function(){

           // Slice the arrat to remove the entry already processed and pass an incremented counter
           processLine(eachLine.slice(1), count+1);
        },10000);
    }
}
// Do the initial call and start the process off
processLine(eachLine,1);

Working snippet below:

I shortened the time delay for this example.

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>

Using a repeated setTimeout() call:

var i = 0;
function processLine(){
    if(eachLine[i++].length > 0){
        doP(eachLine[i], i);
    }
    // if any entries left, process them pseudo-recursively via timer
    if(i 

Post Status

Asked in February 2016
Viewed 3,074 times
Voted 12
Answered 2 times

Search




Leave an answer