Monday, 29 May 2017

cant change a variable due to scope issue

I am new to Node.js and javascript in total.

I am having a problem in this code , I think the variable scope in javascript is different than other languages I am used too.

var ch1_state = "https://i.imgur.com/q1hQolN.png";
var ch2_state = "https://i.imgur.com/q1hQolN.png";
var ch3_state = "https://i.imgur.com/q1hQolN.png";

  cmd.get(
    'gpio -g read 5',
    function(err, data, stderr)
    {

      if (data == 1)
      {
        console.log(data);
        ch1_state = "https://i.imgur.com/Egj3dTW.png";
      }
    }   );

  cmd.get(
    'gpio -g read 27',
    function(err, data, stderr)
    {
      if (data == 1)
      {
        console.log(data);
        ch2_state = "https://i.imgur.com/Egj3dTW.png";
      }
    }   );

  cmd.get(
    'gpio -g read 4',
    function(err, data, stderr)
    {
      if (data == 1)
      {
        console.log(data);
        ch3_state = "https://i.imgur.com/Egj3dTW.png";
      }
    }   );

Although the data variable in the if conditions is sometimes equals to 1. the value of chX_state variable is not changing.

any ideas ??



via Abood Hor

No comments:

Post a Comment