javascript - setTimeout and recursive IFFEs, nested!? How to calculate the delay. -


this problem has reminded me regarding js skills... :(

i feel i'm right on cusp i'm struggling understand conceptually, it's not syntax issue feel cracked it.

what need

  • i'm trying console.log series of strings, 1 letter @ time.
  • there needs delay between each letter outputted, 300ms.
  • there must delay between each string outputted, 2000ms.
  • there 2 strings in example array, solution must support dynamic number of strings.

my current code (can pasted console)

var stringlist = ['first test','second test'],     stringlistlen = stringlist.length;        for(var = 0; < stringlistlen; i++){      // begin first string , read it's length iterations             var stringnumber = 0;     var currentstring = stringlist[stringnumber];     var currentstringlen = currentstring.length;      // see word clarification @ point in code     console.log(currentstring);       (function (i) {         settimeout(function () {                              (var j = 0; j < currentstringlen; j++) {                (function (j) {                 settimeout(function () {                      // magic happens here                     console.log(j, currentstring.charat(j));                      // end of string, read next string, reset letter count                     if(j === currentstringlen - 1){                         stringnumber++;                         currentstring = stringlist[stringnumber];                         j = 0;                                           }                  }, 300 * j); // each letter * specified delay               })(j);             };          }, currentstringlen * 300 * i); // letter * delay * letters in word     })(i);         } 

the issue

the good: getting short delay between letters outputted, , check switch new word , reset letter counter when end of first word working...

the bad: can't wait between 2 words work. have tried few ideas , have got myself confused don't know if approach correct now.

the ugly: final letter of last term not outputting, , totally unexpected.

what i've tried.

okay, i've tried changing "currentstringlen * 300 * i" elements various combinations seemed logical had no effect better or worse. think trying calculate "wait number of letters in current string times 300 (the letter delay) * " <---- strikethrough...

i don't know i'm calculating , that's issue.

i think want split 2 functions, not 2 nested ones. 1 read , pass in string function outputs letters short delay, once gets last letter calls first function asking next word. i'm still going need recurse number of strings in array creates same issue...

am missing fundamental here people?

is had in mind?

function printletters(stringlist) {      var wordindex = 0,          letterindex = 0;        printnext();        function printnext() {          var word = stringlist[wordindex];          var letter = word.charat(letterindex);            console.log(letter);            ++letterindex;            if (letterindex === word.length) {              letterindex = 0;              ++wordindex;                if (wordindex < stringlist.length) {                  settimeout(printnext, 2000);              }                            return;          }            settimeout(printnext, 300);      }  }    printletters(['first test', 'second test']);

here there's ever 1 settimeout running @ once , new 1 being set required appropriate time.

while don't recommend having multiple timers running @ once, can done. this:

function printletters(stringlist) {      var lettercount = 0,          starttime = date.now();        stringlist.foreach(function(word, wordcount) {          word.split('').foreach(function(letter) {              settimeout(function() {                  console.log(letter, date.now() - starttime);              }, wordcount * 1700 + (lettercount * 300));                ++lettercount;          });      });  }    printletters(['first test', 'second test']);

here i've included time delta in logging give better sense of going on when. gap between strings 2000 constant in code 1700 because there's 300 being added.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -