我把这个放在我的剧本里:
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函数.我怎么才能让它起作用?
发布于 2016-02-08 10:39:22
使用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>
使用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>
发布于 2016-02-08 11:17:47
由于您希望立即显示第一行,因此需要将代码包装在函数中,以便可以进行初始调用。
另一个示例包含计数器,下面是另一个选项(使用带有递归函数的数组切片)。
注释递归版本:
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>
使用重复的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>
https://stackoverflow.com/questions/35267374
复制相似问题