【问题标题】:For Loop running infinitelyFor循环无限运行
【发布时间】:2019-10-08 20:36:44
【问题描述】:

所以我已经与这个 for 循环斗争了一天半,当我让它实际打印时它会无限运行,即使 if 语句 (logCount === 10) 得到满足...... 不太清楚该尝试什么,真的感觉它比我尝试的要简单得多......

感谢任何解决方案的尝试..

var timers = [];

var log = (function(outputFunc) {

  var counter = 0;
  var callerLog = [];
  var dateTime = [];

  //assigning current Date to a variable to print for logs
  let logDate = new Date();
  return function(timer) {
    for (var logCount = 0; logCount <= 10; logCount++) {
      //printing data without using printFunc, as specified
      document.getElementById("output").innerHTML += logCount + " " + timer + " " + logDate + "<br>";

      //TODO: add after for loop is resolved.
      if (logCount >= 10) {
        clearInterval(timer1);
        clearInterval(timer2);
        clearInterval(timer3);
        document.getElementById("output").innerHTML += "<br><br/> Logging stopped.";
      }
    }
  }
})(printFunc);

function printFunc(output) {
  document.write(output + "<br>");
}

function startMeUp() {
  // add each of the timer references to the timers array
  // as part of invoking the log function following each interval
  timers.push(setInterval("log('Timer1')", 1000));
  timers.push(setInterval("log('Timer2')", 1200));
  timers.push(setInterval("log('Timer3')", 1700));
}

【问题讨论】:

  • 这段代码的目的是什么?
  • 为什么要将字符串传递给 setInterval ?
  • 我在此处提供的代码中唯一能看到的是clearInterval 永远不会工作,因为从未定义过timer1timer2timer3。那,printFunc 将擦除整个页面,这可能导致document.getElementById 开始抛出空指针异常。但是,for 循环应该在运行 11 次后正确终止。如果有的话,从这里的代码来看,我希望它会因为错误而更快地完成。尽管由于setIntervals,它可能会永远重新运行
  • 你的代码没有意义。您正在将字符串传递给 setInterval 函数,并且没有声明您要清除的任何计时器。
  • 我的教授给了我这个代码 lmao 谢谢大家,很明显是被窃听了,哈哈,都修好了,谢谢,真的是在浪费我的时间

标签: javascript function loops for-loop


【解决方案1】:

我猜这就是你想要实现的目标:

function printFunc(output) {
    // Replaced with console.log for my own convenience
    console.log(output);
}

// Not really a factory, just a curry of the outputFunc
function loggerFactory(outputFunc) {
    return function startLogger(name, interval) {
        // Variables to keep track of the iterations and a reference to the interval to cancel
        let logCount = 0;
        let intervalRef;

        function tick() {
            // On each tick, check if we're done
            // If yes, clear the interval and do the last output
            // If no, do some output and increment the iterator
            // Once the next tick passes we'll check again
            // If you were using setTimeout instead you would have to requeue the tick here
            if (logCount >= 10) {
                clearInterval(intervalRef);
                outputFunc('Done ' + name);
            } else {
                outputFunc(logCount + " " + name);
                logCount += 1;
            }
        }

        // Start it of
        intervalRef = setInterval(tick, interval);
    }
}

const logger = loggerFactory(printFunc);

function startMeUp() {
    console.log('Starting');
    logger('Log1', 1000);
    logger('Log2', 1200);
    logger('Log3', 1700);
}
startMeUp();

一些注意事项: 您可以将 intervalRefs 推送到一个数组中,但我发现将这项工作封装在同一个记录器中会更好,因为它应该只会自行清理。

在使用间隔(或一般的异步代码)时,for 循环通常不是您想要的。 for 循环本质上是同步的,所有迭代将直接在彼此之后运行,没有其他空间。您正在寻找的是一种同时运行多个异步“轨道”的方法。您可以使用 for 循环来启动这些“轨道”,例如:

 for () {
   logger('Log' + i, 1000 * i);
 }

但关键在于logger快速设置了区间函数,然后返回。这样,您的 for 循环可以快速“调度”任务,但记录器使用 setInterval 或 setTimeout 在内部异步运行迭代。

【讨论】:

  • 欣赏额外的知识,最终使用了一段时间来完成实验室,但始终感谢适用的知识谢谢?
猜你喜欢
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 2017-06-20
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
  • 2017-10-08
相关资源
最近更新 更多