首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >每10秒运行一个函数

每10秒运行一个函数
EN

Stack Overflow用户
提问于 2016-02-08 10:28:28
回答 2查看 461关注 0票数 5

我把这个放在我的剧本里:

代码语言:javascript
运行
复制
for(var i = 0, l = eachLine.length; i < l; i++) {
 if(eachLine[i].length>0){
  doP(eachLine[i], +i);
 }
}

从字符串中读取行的for并调用doP函数。所发生的是它是太快,并造成一些速度问题,在我的网页上取决于文字大小。

我想每10秒调用一次doP函数.换句话说,我想再等10秒再调用doP函数.我怎么才能让它起作用?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-02-08 10:39:22

使用setInterval()

代码语言:javascript
运行
复制
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();

代码语言:javascript
运行
复制
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/>");
}
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

使用setTimeout()

代码语言:javascript
运行
复制
var i = 0, len = eachLine.length;
function looper(){
    if(eachLine[i].length > 0)
        doP(eachLine[i], ++i);
    if(i < len) 
        setTimeout(looper, 10000);
}
looper();

代码语言:javascript
运行
复制
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/>");
}
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

票数 3
EN

Stack Overflow用户

发布于 2016-02-08 11:17:47

由于您希望立即显示第一行,因此需要将代码包装在函数中,以便可以进行初始调用。

另一个示例包含计数器,下面是另一个选项(使用带有递归函数的数组切片)。

注释递归版本:

代码语言:javascript
运行
复制
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);

下面是工作片段:

我缩短了这个例子的延迟时间。

代码语言:javascript
运行
复制
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);
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

使用重复的setTimeout()调用:

代码语言:javascript
运行
复制
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();

代码语言:javascript
运行
复制
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/>");
}
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35267374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档