Run a function inside ON every 10 seconds

I have this for my script:

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

to read lines from a line and call the doP function. What happens is too fast and causes some speed problems on my website depending on the size of the text.

I want to call the doP function every 10 seconds ... In other words, I want to wait 10 seconds to call the doP function again ... how can I make it work?

+4
source share
2 answers

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>
Run codeHide result

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://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
Run codeHide result
+2
source

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 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);

:

.

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 result

setTimeout():

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 < eachLine.length) {
        setTimeout(processLine, 10000);
    }
}
// Run initial first line immediately
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
+1

Source: https://habr.com/ru/post/1627771/


All Articles