回调中的Javascript函数控件

时间:2016-02-20 00:45:41

标签: javascript callback

我不确定我是否完全获得了javascript。我有三个函数,它们互相调用一个,两个和三个..但如果两个条件为真,我不希望三个函数运行。由于某种原因,它继续运行功能三。如何根据功能2中的条件确保功能三不运行。抱歉,如果已经在某处已经回答但是我找不到它...我已经看了看。我想我的脑袋即将爆炸..谢谢你。

这是我配置代码的基本示例:

gpio = require("rpi-gpio");

gpio.setup(35, gpio.DIR_OUT, dooropen);  //motor will raise the door
//gpio.setup(37, DIR_OUT, CLOSEDOOR); //motor will lower the door
gpio.setup(40, gpio.DIR_IN, TopHall);  //Top Hall Effect Sensor
gpio.setup(38, gpio.DIR_IN, BottomHall); // Bottom Halle Effect Sensor


function dooropen(err, value){
  if (err) {
    console.log('Some type of error');
  } else {
  //first check door is closed on gpio 38
  //use function doorClosedCallback which receives (err, value) arguments from DOORCLOSED gpio pin
    gpio.read(38, BottomHall);
  }
}

function BottomHall(err, value) {
  if (err) {
    console.log('Error checking BottomHall.');
  } else if (value === true) {
    console.log('Door is not closed.');
    return;
    } else {
    gpio.write(35, true, TopHall);
    console.log('Door will open now.');
  }
}

function TopHall(err, value) {
  if (err) {
    console.log('Door open failed to start.', err);
    return;
  } else {
    function readInput() {
    gpio.read(38, function(err, value) {
        console.log('The value is ' + value);
    });
    }
    var doorinvteralopen = setInterval(function(){
      gpio.read(40, function(err, value) {
        if (err) {
          console.log('Door start failed.', err);
          return;
        } else if (value === true) {
              console.log('Door not yet open, keep the motor running.');
        } else {
          clearInterval(doorinvteralopen);
          console.log('Door is finally open.');
          gpio.write(35, false, function(err) {
            if (err) {console.log('Error stopping the door motor.', err);
            }
          });
        }
      });
    }, 1000);
  }
}

//CHANGE I WILL ADD TO ABOVE
function TopHall(err, value) {
     gpio.read(38, function(err, value){
       if(value !== false){
       console.log('No Hall Effect Sensors available, com stopped');
       return;
      }
     });
     var doorinvteralopen = setInterval(function(){...

1 个答案:

答案 0 :(得分:-3)

如果我正确地理解了你的问题,可以用函数二中的一个简单的else来解决这个问题,使它看起来像这样:

function two(twoParam) {
  if (twoParam === true) {
    return;
  }

  else {
    var threeParam = ...;
    three(threeParam);
  }
}