【问题标题】:Unable to receive value from switch statement inside loop无法从循环内的 switch 语句接收值
【发布时间】:2018-10-30 09:13:28
【问题描述】:

我遇到了这个问题,我创建的循环没有返回值。

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

谢谢,希望我不会错过发布此消息。

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

【问题讨论】:

  • 有什么问题?
  • 另外,这里需要switch吗? if-else 怎么样?
  • 即使解决了这种情况,我还可以添加另一个问题。如何暂停一个案例的执行5秒(对于火车到站的案例)。 setTimeout 似乎对我不起作用..
  • @vivek_23 为什么我会选择 if-else,因为我有可能火车在 A 站(重点是,当我可以保存时,为什么要运行整个 if-else-if-else一些时间和内存使用开关)?

标签: javascript arrays oop for-loop switch-statement


【解决方案1】:

再试一个:

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

【讨论】:

  • 不错的一个!我希望我能完全理解没有任何 googlein 的代码,但我会搜索这些语句。谢谢!
【解决方案2】:

试试这个

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

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多