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

问题描述 投票:-1回答: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, '', '', '', '', '', '');
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);
javascript arrays oop for-loop switch-statement
2个回答
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);

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';
}

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

希望,这对你有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.