Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 设置超时混乱_Javascript_Jquery_Settimeout - Fatal编程技术网

Javascript 设置超时混乱

Javascript 设置超时混乱,javascript,jquery,settimeout,Javascript,Jquery,Settimeout,我有一个数组words,里面有单词。我想显示每秒说“N”的字数。我写了下面的代码,它只适用于第一个实例。请让我知道 tempstr = ""; for(k=0;k<grupof;k++) tempstr += words[k] + " "; i = grupof; jQuery(document).ready(function(){ (function insertArray(){ $("p").text(tempstr); if(i

我有一个数组
words
,里面有单词。我想显示每秒说“N”的字数。我写了下面的代码,它只适用于第一个实例。请让我知道

tempstr = "";
for(k=0;k<grupof;k++)
    tempstr += words[k] + " ";
i =  grupof;
jQuery(document).ready(function(){
    (function insertArray(){
         $("p").text(tempstr); 
         if(i <words.length){
              setTimeout(insertArray, 2000);
              tempstr = "";
              for(j=i;j<grupof;j++)
                  tempstr += words[j] + " ";
              i = j;
         }
    })();
});
tempstr=”“;

对于(k=0;k),您可能需要考虑使用<代码> SETIFATION< /COD>。下面是一个工作示例:

var strn = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).";

var wordsPer = 2;
var duration = 500;

function showWords(words) {
    for (var i = 0; i < wordsPer; i++) {
        if (words.length > 0) {
            el.innerHTML += ' '+ words.shift();
        }
    }
    if (words.length == 0) {
        clearInterval(intervalID);
    }
}

var words = strn.split(" ");
var el = $("p")[0];
var intervalID = setInterval(function() {
    showWords(words);
}, duration);
var strn="长期以来,读者在查看页面布局时会被页面的可读内容分散注意力是一个既定事实。使用Lorem Ipsum的意义在于,它的字母分布或多或少是正常的,而不是使用“此处内容,此处内容”,使其看起来像可读的英语。许多桌面发布软件包和web pa通用电气的编辑们现在使用Lorem Ipsum作为他们的默认模型文本,搜索“Lorem Ipsum”将发现许多尚处于起步阶段的网站。多年来,各种版本不断演变,有时是偶然的,有时是故意的(注入幽默等)。”;
var-wordsPer=2;
var持续时间=500;
函数showWords(words){
对于(var i=0;i0){
el.innerHTML+=''+words.shift();
}
}
if(words.length==0){
clearInterval(intervalID);
}
}
var words=strn.split(“”);
var el=$(“p”)[0];
var intervalID=setInterval(函数(){
showWords(单词);
},持续时间);
var words=新数组(“word1”、“word2”、“word3”、“word4”、“word5”、“word6”、“word7”);
$(文档).ready(函数(){
函数插入数组(组、索引){
var tempstr=“”;
如果(组+索引>单词长度){
if(words.length-index
for(j=i;j这里有一个工作JSFIDLE:


“仅适用于第一个实例”是什么意思?请创建一个演示。小提琴在未定义变量
grupof
上失败:(没有解释您更改了什么以及为什么?您的主要问题是最后一个for循环。索引没有重置,因此它永远不会超过第一个组。我已在修改的函数中处理了这一点。此外,我还添加了一个catch,以显示计时器最后一次迭代的部分组。
var words = new Array("word1", "word2", "word3", "word4", "word5", "word6", "word7"); 

$(document).ready(function(){

    function insertArray(group, index){
        var tempstr = "";

        if(group+index > words.length){
             if(words.length - index <= 0)
                 return;
             else
                 group = words.length - index;
        }

        for(var k=0;k<group;k++)
            tempstr += words[k + index] + " ";

         index += group;             
         $("p").text(tempstr); 
         if(index < words.length)
             setTimeout(function(){insertArray(group, index);}, 2000);
    }
    insertArray(2,0);

});
for(j=i;j<grupof;j++)
for(var j=i;j<grupof + i;j++)
var strn = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).";

var wordsPerInterval = 3,
    duration = 500,
    el = $('p'),
    words = strn.split(' ');

el.html('');

var interval = setInterval(function() {
    var toInsert = words.slice(0, wordsPerInterval);

    words = words.slice(wordsPerInterval, words.length);

    if (toInsert.length)
        el.html(el.html() + toInsert.join(' ') + ' ');
    else
        clearInterval(interval);
}, duration);