开发者

setTimeout confusion

I have an array words and it has words in it. I want to display say 'N' number of words per second. I wrote the code below and it is working for the first instance only. Please let me know.

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(insertArr开发者_如何学编程ay, 2000);
              tempstr = "";
              for(j=i;j<grupof;j++)
                  tempstr += words[j] + " ";
              i = j;
         }
    })();
});

Here is the jsfiddle demo: http://jsfiddle.net/Skg7d/2/


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

needs to be

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


You may want to consider using setInterval instead. Here's a working example:

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


Here's a working 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);


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜