Javascript电梯程序

Javascript电梯程序,javascript,jquery,arrays,Javascript,Jquery,Arrays,我正在用HTML/CSS/Javascript创建一个电梯,在Javascript的某些部分遇到了一个问题 我试图记录每一个按下的按钮,然后让电梯按顺序到达每一层。我目前正在将每个楼层添加到阵列中 每次我运行程序时,电梯都会在每次迭代的整个阵列中运行 如何将每个楼层添加到阵列中,然后访问其中一个楼层,将其移除,程序继续在现有阵列中移动 JavaScript 我真的不明白您想要实现什么,但是由于您使用jQuery,并且希望迭代所有楼层,所以可以使用该函数。 e、 g.$(楼层)。每个(功能(){

我正在用HTML/CSS/Javascript创建一个电梯,在Javascript的某些部分遇到了一个问题

我试图记录每一个按下的按钮,然后让电梯按顺序到达每一层。我目前正在将每个楼层添加到阵列中

每次我运行程序时,电梯都会在每次迭代的整个阵列中运行

如何将每个楼层添加到阵列中,然后访问其中一个楼层,将其移除,程序继续在现有阵列中移动

JavaScript
我真的不明白您想要实现什么,但是由于您使用jQuery,并且希望迭代所有楼层,所以可以使用该函数。 e、 g.
$(楼层)。每个(功能(){
//做点什么
};

哦,顺便说一句,我看到您使用了push,您可以使用
pop()
提取该数组中的第一个对象。

这对我来说只是尖叫“状态机”,所以我强烈建议您至少阅读一些

但是,对于您手头的问题,当电梯停止移动时,您需要做一些工作。我看到您有一个
isMoving
标志,因此当该标志设置为false时,您从
队列到达的楼层开始移动到阵列中的下一个目的地


重复此操作,直到队列
数组为空。

您可以这样定义电梯

var Elevator = (function Elevator() {

  var floors = 10,
  queue = [],
  current = 1, 
  isMoving = false,
  destination;

  // This gets the floor being clicked and adds it to the queue
  // if the elevator is stationary and the floor is not the current floor,
  // or if the elevator is in motion and the floor is not the most recently requested floor.
  // It also sets the elevator in motion if a floor was added to the queue.
  function getFloors(event) {
    event.preventDefault();
    destination = $(this).attr('href');
    console.log(destination);
    var repeatedFloor = isMoving ? queue[queue.length - 1] : current;
    if (destination !== repeatedFloor){
      queue.push(destination);
      if (!isMoving) {
        isMoving = true;
        setTimeout(moveFloors, 3000);
      }
      console.log(queue);
    }
  }

  // This completes a move for the elevator (sets the current floor)
  // and either halts the elevator or continues its motion to the
  // next floor in the queue.
  function moveFloors() { 
    console.log(queue);
    current = queue.shift();
    console.log("moved to floor " + current);
    if (queue[0] !== undefined) {
      setTimeout(moveFloors, 3000);
    } else {
      isMoving = false;
    }
  }

  // this acts as a controller
  // for all events and how and what we bind them to
  function bindEvents() {
    $('.btn').on('click', getFloors);
  }
  // this runs the bind events
  // and only runs the bind events
  function init(){
    bindEvents();
  }
  // this makes the init method public for invoking
  return {
    init: init()
  };
})();
getFloors
方法处理确定是否将楼层添加到队列中的逻辑,如果以前没有,现在需要,则启动电梯移动(使用
setTimeout
-您可能有其他类型的触发器)。根据队列的状态,
moveFloors
方法更新当前楼层,并停止电梯移动或将其设置到下一层。此解决方案使用
shift
将楼层从队列的前面拉出

代码笔在这里:


我相信这可能需要大量的清理,您可能需要修改它以适应您的特定情况,但希望这是一个开始。

也许您希望使用更高级的数据结构,以便更轻松地添加和删除元素。如果这是您的难题(?))


可能是某个链表

我是否也可以使用
shift
来切断数组的第一项?此外,它的设置方式每次都会添加到数组中,这会强制动画在每次单击时触发。是的,
shift
也可以。您可能只需要一个if检查来确定动画是否应该触发
if(isMoving)//添加到队列,但不设置动画;否则//开始动画
.pop()
实际上接受最后一个元素,而不是第一个元素,即
.unshift()
,尽管
pop
可能更有意义。实际上,它弹出了最后一个索引:我被一个堆栈弄糊涂了:)
var Elevator = (function Elevator() {

  var floors = 10,
  queue = [],
  current = 1, 
  isMoving = false,
  destination;

  // This gets the floor being clicked and adds it to the queue
  // if the elevator is stationary and the floor is not the current floor,
  // or if the elevator is in motion and the floor is not the most recently requested floor.
  // It also sets the elevator in motion if a floor was added to the queue.
  function getFloors(event) {
    event.preventDefault();
    destination = $(this).attr('href');
    console.log(destination);
    var repeatedFloor = isMoving ? queue[queue.length - 1] : current;
    if (destination !== repeatedFloor){
      queue.push(destination);
      if (!isMoving) {
        isMoving = true;
        setTimeout(moveFloors, 3000);
      }
      console.log(queue);
    }
  }

  // This completes a move for the elevator (sets the current floor)
  // and either halts the elevator or continues its motion to the
  // next floor in the queue.
  function moveFloors() { 
    console.log(queue);
    current = queue.shift();
    console.log("moved to floor " + current);
    if (queue[0] !== undefined) {
      setTimeout(moveFloors, 3000);
    } else {
      isMoving = false;
    }
  }

  // this acts as a controller
  // for all events and how and what we bind them to
  function bindEvents() {
    $('.btn').on('click', getFloors);
  }
  // this runs the bind events
  // and only runs the bind events
  function init(){
    bindEvents();
  }
  // this makes the init method public for invoking
  return {
    init: init()
  };
})();