2011-08-17 54 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);