2011-08-17 82 views
1

我有一个阵列words它有它的话。我想每秒钟显示'N'字数。我写了下面的代码,它仅适用于第一个实例。请告诉我。setTimeout confusion

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/

+2

你是什么意思与*“它只为第一个实例工作”*?请创建一个http://jsfiddle.net/演示。 –

+0

jsfiddle演示的+1。 – Skilldrick

+0

小提琴在未定义变量'grupof'上失败。 :( –

回答

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

需求是

for(var j=i;j<grupof + i;j++) 
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

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/

+0

没有说明你改变了什么,为什么? – jfriend00

+0

你的主要问题是你的最后一个循环,索引没有被重置,所以它永远不会超过第一组。在我的修改函数中。另外,我添加了一个catch来显示最后一次迭代计时器的部分组。 –

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