2012-07-27 4760 views
8

我正在構建循環文本生成器。生成器將來自多個數組的句子(文本)組合在一起,通過視覺「循環」並添加它們。我認爲最好創建一個basic version of the generator小提琴,因爲我已經建造現在:循環文本生成器

說明

的基本工作原理如下:

  1. 句在單獨的數組定義(Array1Array2Array3
  2. 第二組陣列被定義,包含可以組合的陣列(combo0和在小提琴)
  3. 在按下「生成」按鈕,則該函數Generate被調用,它在視覺上從句子中的小提琴的陣列(combo0[0]週期句子)
  4. 此函數循環本身直到句話循環8次(var times = 8在提琴)
  5. 當這樣做完成後,該函數調用提供的回調函數。在此回調,再次運行Generate,這一次與第二陣列(combo0[1]在小提琴)

的原因回調,我需要「等待」的循環作用來完成,然後移動上。

問題

雖然這不正是我所需要的(並且除了這個事實,我如果這是做到這一點非常令人懷疑;我總是寫一個函數時感覺有點奇怪循環本身),我有以下問題:

combo數組中,我定義哪個'句子'數組可以是可能的組合。如果有兩個組合,但是具有多於兩個這樣工作得很好,我有一個問題:

Generate(combo0[0], i, function(i) { //generate from first array element of combo0, callback to generating from second array of combo0 
    Generate(combo0[1], i, function(i) { 
     $('div#status').html('Done!'); //show status 
     $('input#generate').removeAttr("disabled"); //enable button 
    }); 
}) 

我不得不遞歸改寫這個容納combo陣列由3或甚至4的可能性選項。如果combo數組僅包含2個(或1個)數組,則可能會破壞腳本。

這是我卡住的地方。主要問題是,如果我遍歷combo陣列,例如與.each(); generate函數被同時調用多次,所以整個'循環'效果丟失。

我試過編寫各種循環,它考慮到給定的combo數組的長度,但今天比以前我的瀏覽器崩潰了很多,我不知道該怎麼做。

+0

因此,你需要一個腳本,可以通過多個數組,一次一個項目,直到它到達數組中的某個項目,然後進展到下一個數組,並以可視方式呈現每個步驟? – Luka 2012-07-27 14:03:16

+0

那麼,不是某個項目;一個數組中的隨機項。它需要打印一個數組項目x次,然後最後寫下一個並繼續到下一個數組。基本上,它需要做的一切就是在Fiddle中工作(請參閱我的文章),除了包含2個以上數組的數組。 – 2012-07-27 14:14:31

+1

_我認爲最好創建一個Fiddle_ - 是的! +1 – halfer 2012-07-27 14:22:12

回答

0

我設法解決它。離屏幕有一段距離是件好事。

我所做的是增加一個「N」計數器,如果達到times變量的多重增加,導致功能繼續迭代,但輸出(見第三至最後一行),從下一個陣列(lists[n] )。最後,查看剩下多少數組將確定我們是否完成。如果完成,最後一次寫入句子,運行可選回調並返回false。這樣一來,該函數將接受整個數組,而不僅僅是子陣(combo而不是combo[0]):

//Generate from array 'lists', simulating a 'slot machine 
function Generate(lists, n, i, callbackFnk) { 
    if (!(i >= 0)) { i= 0; } 
    setTimeout((function(msg){ 
     i++; 
     return function(){ 
      if (i % times != 0){ 
       //We haven't reached a multiple of the times variable yet, keep going. 
       Generate(lists, n, i, callbackFnk); 
      } else if (i % times === 0 && i != 0 && n < (lists.length-1)) { 
       //Multiple reached, increase n 
       n++; 
       Generate(lists, n, i, callbackFnk); 
      } else if (n == (lists.length-1)) { 
       //we are done as there are no more arrays to go over 
       showMessage(msg, i); 
       if (callbackFnk) callbackFnk.call(this, i); 
       return false; 
      } 
      showMessage(msg, i); 
     } 
    })(
     //output using the given n value 
     lists[n][Math.floor(Math.random() * lists[n].length)] 
    ), speed); 
} 

見工作小提琴這裏:http://jsfiddle.net/c_kick/kuNrA/1/

我希望這可以幫助別人!

1

這應該做的伎倆:

var Array1 = new Array("Sentence 1 - A ", "Sentence 1 - B ", "Sentence 1 - C ", "Sentence 1 - D ", "Sentence 1 - E ", "Sentence 1 - F ", "Sentence 1 - G "); 
var Array2 = new Array("Sentence 2 - A", "Sentence 2 - B", "Sentence 2 - C", "Sentence 2 - D", "Sentence 2 - E", "Sentence 2 - F", "Sentence 2 - G"); 
var Array3 = new Array("Sentence 3 - A", "Sentence 3 - B", "Sentence 3 - C", "Sentence 3 - D", "Sentence 3 - E", "Sentence 3 - F", "Sentence 3 - G"); 

//define acceptable combinations of arrays 
var combo0 = new Array(Array1, Array2); 
var combo1 = new Array(Array1, Array2, Array3); 

//define global vars 
var speed = 140; 
var content = ''; 

//write sentence to box. If counter is a multiple of the 'times' var: append instead of write 
function showMessage(list, i, e) { 
    $('div#generated').html(content + list[i]); 
    if ((i + 1) % (e + 1) == 0) content += list[i]; 
} 

//Generate from array 'list', simulating a 'slot machine 
function Generate(lists, end, l, i, e) { 
    if (l == undefined) l = 0; 
    if (i == undefined) i = 0; 
    if (e == undefined) e = Math.floor(Math.random() * lists[l].length); 
    setTimeout(function() { 
     showMessage(lists[l], i, e); 
     if ((i + 1) % (e + 1) != 0) 
      Generate(lists, end, l, i+1, e); 
     else if (lists.length - 1 > l) 
      Generate(lists, end, l + 1); 
     else end(); 
    }, speed); 
} 

$(document).ready(function() { 
    $('input#generate').click(function() { 
     $(this).attr("disabled", true); //disable button 
     content = ''; //reset content 
     $('div#status').html('Running..'); //show status 
     Generate(combo0, function() { 
      $('div#status').html('Done!'); //show status 
      $('input#generate').removeAttr("disabled"); //enable button}); 
     }); 
    }); 
}); 
+0

哇,這個工程,只是看起來'不穩定';它不會在每個數組上「穩定地循環」(我注意到你放棄了'times'變量)。有時第一個數組項目只顯示,第二個將循環5次,第三個循環2次。我會看看,看看你是如何管理它,看看我能不能解決它。雖然謝謝! – 2012-07-27 19:44:24

+0

唉,我不知道如何解決它;我需要它是穩定的(總是爲每個數組項目循環一定數量)。再次感謝您的建議! – 2012-07-27 20:02:28

+0

我試圖通過將速度降低到每秒1滴(速度= 1000)來調查它,並且我沒有看到任何規律性,它可能僅僅是由於速度太高而造成的。 – Luka 2012-07-27 20:52:09