无法从循环内的switch语句接收值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法从循环内的switch语句接收值相关的知识,希望对你有一定的参考价值。

我遇到这个问题,我从我创建的循环中得不到任何返回值。

这个想法很简单 - 我需要从第二到第二更新我的火车到达的距离。如果达到滚动的秒数,则意味着它正在前往车站,或者恰好在车站中。这是我的代码,如下。

谢谢,希望我不会错过这个帖子。

function Train(speed, distance, elapsedTime, eTA, station, condition, type) {
  this.speed = speed;
  this.station = station;
  this.distance = distance;
  this.elapsedTime = elapsedTime;
  this.eTA = eTA;
  this.condition = condition;
  this.type = this.speed == 30 ? 'Regio' : this.speed == 70 ? 'Inter-Regio' : 'Broken'; 
}

var trainOne = new Train(30, '', '', '', '', '', '');
var elapsedTime = 
  function theLoop (elapsedTime) {
    setTimeout (function(){
    switch(elapsedTime){
      case elapsedTime == 0:
        Train().station = 'Station A';
        Train().eTA = 28 - elapsedTime + 'seconds left untill reaching the next station';
        break;

      case elapsedTime < 28:
        Train().station = 'Train left station A';
        Train().eTA = 28 - elapsedTime + 'seconds left untill reaching the next station';
        break;

      case elapsedTime == 28:
        Train().station = 'Station 2';
        Train().eTA = 28 - elapsedTime + 'seconds left untill reaching the next station';
        break;

      case elapsedTime > 28 && elapsedTime < 70:
        Train().station = 'Train left station 2';
        Train().eTA = 70 - elapsedTime + 'seconds left untill reaching the next station';
        break;

      case elapsedTime == 70:
        Train().station = 'Station 3';
        Train().eTA = 84 - elapsedTime + 'seconds left untill reaching the next station';
        break;

      default :
        Train().station = 'Station A';
        Train().eTA = 'No information available yet';
    }
    console.log(trainOne);
    if (++elapsedTime && elapsedTime < 12) {
      theLoop(elapsedTime);
    }
  }, 1000);
} (0);
答案

尝试另一个:

function Train(speed, distance, elapsedTime, eTA, station, condition, type) {
  this.speed = speed;
  this.station = station;
  this.distance = distance;
  this.elapsedTime = elapsedTime;
  this.eTA = eTA;
  this.condition = condition;
  this.type = this.speed == 30 ? 'Regio' : this.speed == 70 ? 'Inter-Regio' : 'Broken';

  let timerID = null;

  this.run = function() {
    timerID = setInterval((self) => {
      self.elapsedTime++;
      self.distance += speed;
      console.log(`${self.elapsedTime} seconds, distance: ${self.distance}`);
    }, 1000, this);
  }

  this.stop = function() {
    console.log("Stop");
    clearInterval(timerID);
  }

  this.pause = function() {
    clearInterval(timerID);
  }

  this.station = function(name, duration) {
    console.log(`Station ${name}`);

    this.pause();

    setTimeout((self) => {
      console.log(`Live station ${name}`);
      self.run();
    }, duration, this, name);
  }
}

var trainOne = new Train(30, 0, '', 0, '', '', '');

trainOne.run();

setTimeout(() => {
  trainOne.station("A", 5000);
}, 5000);

setTimeout(() => {
  trainOne.station("B", 5000);
}, 15000);

setTimeout(() => {
  trainOne.stop();
}, 30000);
另一答案

试试这个

function Train(speed, distance, elapsedTime, eTA, station, condition, type) {
  this.speed = speed;
  this.station = station;
  this.distance = distance;
  this.elapsedTime = elapsedTime;
  this.eTA = eTA;
  this.condition = condition;
  this.type = this.speed == 30 ? 'Regio' : this.speed == 70 ? 'Inter-Regio' : 'Broken';
}

var trainOne = new Train(30, 0, '', 0, '', '', '');

function theLoop(elapsedTime) {
  setTimeout(function() {

    trainOne.distance += trainOne.speed * elapsedTime;

    if (elapsedTime === 0) {
      trainOne.station = 'Station A';
      trainOne.eTA = 28 - elapsedTime + 'seconds left untill reaching the next station';
    } else if (elapsedTime < 28) {
      trainOne.station = 'Train left station A';
      trainOne.eTA = 28 - elapsedTime + 'seconds left untill reaching the next station';
    } else if (elapsedTime === 28) {
      trainOne.station = 'Station 2';
      trainOne.eTA = 28 - elapsedTime + 'seconds left untill reaching the next station';
    } else if (elapsedTime > 28 && elapsedTime < 70) {
      trainOne.station = 'Train left station 2';
      trainOne.eTA = 70 - elapsedTime + 'seconds left untill reaching the next station';
    } else if (elapsedTime === 70) {
      trainOne.station = 'Station 3';
      trainOne.eTA = 84 - elapsedTime + 'seconds left untill reaching the next station';
    }

    console.log(elapsedTime);
    console.log(trainOne.station);

    if (elapsedTime < 100) {
      theLoop(++elapsedTime);
    }

  }, 1000);
};

theLoop(0);

以上是关于无法从循环内的switch语句接收值的主要内容,如果未能解决你的问题,请参考以下文章

使用从循环内的代码片段中提取的函数避免代码冗余/计算开销

2-23分支语句(switch…case)及循环语句

switch语句和for循环

流程控制

利用switch语句:数字1—7分别表示周一到周日,随机输入数字后输出对应的星期

java 循环变量