fatboy95
Posts: 30
Joined: Thu Jul 16, 2015 9:02 pm

How to stop a function

Wed Feb 17, 2016 6:14 pm

I have 3 functions defined.. the first calls the second and second calls the third. The 1st and 2nd will always run but the 3rd will only run if the 2nd value is false. If the 2nd value is true it should stop and return, however, the third function is still running in full.

It works perfect if gpio 38 is false so I know my script is good in design.

below is example of the code I am using for the 2nd function and I am using node and JavaScript.

Thanks for any help.

function second(err, value){
if (value === true){ //this is the value of gpio second pin 38
console.log('Your script stopped due to a true reading');
return;
}else{
gpio.write(35, true, third);
}
}

User avatar
kolban
Posts: 143
Joined: Fri Dec 04, 2015 1:45 am
Location: Texas, USA

Re: How to stop a function

Thu Feb 18, 2016 5:42 am

If I am reading your post correctly, what you want is something like:

Code: Select all

function one(oneParam) {
  var twoParam = ...;
  two(twoParam);
}

function two(twoParam) {
  if (twoParam === true) {
    return;
  }
  var threeParam = ...;
  three(threeParam);
}

function three(threeParam) {
  // do something;
}

one(someValue);
FREE book on Raspberry Pi usage and programming

https://leanpub.com/pi

fatboy95
Posts: 30
Joined: Thu Jul 16, 2015 9:02 pm

Re: How to stop a function

Thu Feb 18, 2016 4:50 pm

You are exactly correct and this is basically how I wrote my code, however, when it runs param 2 it doesn't stop at the return if true.. it keeps running param 3 for some reason.

It seems like if param 2 is equal to true it should return and stop running anything below that but it doesn't.

Return to “Java”