setTimeout混乱

时间:2011-08-17 15:54:55

标签: 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.length){
              setTimeout(insertArray, 2000);
              tempstr = "";
              for(j=i;j<grupof;j++)
                  tempstr += words[j] + " ";
              i = j;
         }
    })();
});

以下是jsfiddle演示:http://jsfiddle.net/Skg7d/2/

4 个答案:

答案 0 :(得分:0)

您可能需要考虑使用setInterval。这是一个有效的例子:

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

fiddle

答案 1 :(得分:0)

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

});

http://jsfiddle.net/reesewill/8ZTYx/

答案 2 :(得分:0)

for(j=i;j<grupof;j++)

需要

for(var j=i;j<grupof + i;j++)

答案 3 :(得分:0)

这是一个有效的JSFiddle: http://jsfiddle.net/Skg7d/19/

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