javascript - setTimeout 和递归 IFFE,嵌套!?如何计算延迟。

标签 javascript loops recursion

这个问题确实让我想起了我对 JS 技能的了解......:(

我觉得我正处于风口浪尖,但我很难理解概念上要做什么,这与其说是语法问题,不如说我觉得我几乎破解了它。

我需要什么

  • 我正在尝试 console.log 一系列字符串,一次一个字母。
  • 每个 LETTER 输出之间需要有一个延迟,比如 300ms。
  • 每个 STRING 输出之间必须有一个延迟,例如 2000 毫秒。
  • 示例数组中有 2 个字符串,但解决方案必须支持动态数量的字符串。

我当前的代码(可以粘贴到控制台中)

var stringList = ['first test','second test'],
    stringListLen = stringList.length;      

for(var i = 0; i < stringListLen; i++){

    // begin with first string and read it's length for iterations        
    var stringNumber = 0;
    var currentString = stringList[stringNumber];
    var currentStringLen = currentString.length;

    // just see the word for clarification at this point in code
    console.log(currentString);


    (function (i) {
        setTimeout(function () {                

            for (var j = 0; j < currentStringLen; j++) {

              (function (j) {
                setTimeout(function () {

                    // THE MAGIC HAPPENS HERE
                    console.log(j, currentString.charAt(j));

                    // End of string, so 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);      


}

问题

好处:我成功地获得了输出字母之间的短暂延迟,并且当我们到达第一个单词的末尾时,我切换到新单词并重置字母计数器的检查正在工作......

坏处:我无法让两个词之间的等待发挥作用。我尝试了一些想法,但让自己很困惑,我不知道我现在的方法是否正确。

丑陋的:最后一个术语的最后一个字母也没有输出,这完全出乎意料。

我尝试过的。

好吧,我尝试简单地将“currentStringLen * 300 * i”元素更改为各种看似合乎逻辑的组合,但没有更好或更坏的效果。最终我想我正在尝试计算“等待当前字符串中的字母数乘以 300(字母延迟)*”<---- STRIKETHROUGH...

我实际上不知道我在计算什么,这就是问题所在。

我现在想我想把它分成两个函数,而不是两个嵌套函数。一个读取字符串并将其传递给另一个函数,该函数仅在短时间内输出字母,然后一旦到达最后一个字母,它就会调用第一个函数,询问下一个单词。但是我仍然需要递归数组中的字符串数量,这会产生相同的问题......

我是否遗漏了一些基本的东西?

最佳答案

这大概是您的想法吗?

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

这里一次只有一个 setTimeout 运行,并且根据需要在适当的时间设置一个新的。

虽然我不建议同时运行多个计时器,但这是可以做到的。像这样的事情:

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

在这里,我在日志记录中包含了时间增量,以便更好地了解何时发生的情况。字符串之间的间隙是 2000,但代码中的常量是 1700,因为已经添加了 300。

关于javascript - setTimeout 和递归 IFFE,嵌套!?如何计算延迟。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46164208/

相关文章:

javascript - Node 'readline' 模块没有 'end' 事件 - 当没有更多行时我该怎么做?

javascript - 是否可以通过 Javascript 访问 %-width 元素的当前缩放因子而不会导致回流?

javascript - 使用 AJAX 和 PHP 上传文件 - $_FILES 数组为空

java - 用循环存储数组并比较最高值和最低值

sql - 防止递归 CTE 多次访问节点

javascript - 如何使用 loadDataWithBaseURL() 方法从 SDCARD 加载 JS 和 CSS

php - 如何使用 PHPExcel 合并 Excel 中的列

PHPMailer:大量电子邮件 - 一次发送所有可变消息,而不是单独发送

algorithm - 递归:打印隐含的括号

python - 如何使用递归反转字符串?