I am creating a rotating text generator. The generator combines sentences (text) from a number of arrays, "loops" through them visually and adds them. I thought it was best to create a script with the base version of the generator , since I just created it:
Explanation
The main operations are as follows:
- Sentences are defined in separate arrays (
Array1 , Array2 and Array3 in the script) - A second set of arrays is defined containing arrays that can be combined (
combo0 and combo1 in the fiddle) - When you click the Create button, the Generate function is called, which visually loops the sentences from the array of sentences (
combo0[0] in the script) - This function executes the loop until the sentence is cyclically 8 times (
var times = 8 in the fiddle) - When this is done, the function calls the callback function that was provided. In this callback, it starts
Generate again, this time with a second array ( combo0[1] in the fiddle)
The reason for the callback is that I need to βwaitβ to complete the loop effect, and then move on.
Problem
Although this does exactly what I need (and besides the fact that I very much doubt that this is the way to do it, I always feel a little strange when writing a function that loops), I have the following problem
In combo arrays, I determine which of the 'sentence' arrays may be possible combinations. This works fine if there are two combinations , but with more than two, I have a problem:
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 }); })
I would have to rewrite this recursively to take into account the possibility of a combo array consisting of 3 or even 4 options. This will probably break the script if the combo array contains only 2 (or 1) arrays.
This is where I am stuck. The main problem is that if I loop over the combo array, for example with .each (); the Generate function is called several times synchronously, so the whole effect of the looping is lost.
I tried to write various loops that take into account the length of the array of the given combo array, but today I have broken more browsers than ever before and I canβt figure out what to do.
javascript jquery arrays
c_kick
source share