Why is my variable still undefined?

lastbalanceis never defined, and because of this, the getBalance function never does what it should have. I thought this could be an asynchronous issue, but it could only be up to the first interval, right? Any ideas?

var lastbalance;
getBalance = function (lastbalance, callback){
  btcclient.getBalance('*', 0, function(err, balance) {
    if (err) return console.log(err);
    if (lastbalance != balance || typeof lastbalance == 'undefined'){
      console.log('Last Balance:' + lastbalance);
      var lastbalance = balance;
      updateCauses();
    }
    console.log('Balance:', balance);


    if (typeof callback=='function') callback();

  });
};

setInterval(getBalance, 2000, lastbalance);
+4
source share
3 answers

Two problems.

1 .: You defined lastbalanceas a function parameter ... that created another variable lastbalancein the context of your function ... that replaced the variable declared in the outer scope.

var lastbalance; // your outer variable
getBalance = function (lastbalance, callback) { // weeeee, another lastbalance
    btcclient.getBalance('*', 0, function (err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined') {
            console.log('Last Balance:' + lastbalance);
            var lastbalance = balance;
            updateCauses();
        }
        console.log('Balance:', balance);
        if (typeof callback == 'function') callback();
    });
};

setInterval(getBalance, 2000, lastbalance); //passing lastbalance by value

2 .: You used varto declare another lastbalancein your function. Do not do this; this caused the problem described above.

var lastbalance; // your outer variable
getBalance = function (lastbalance, callback) {
    btcclient.getBalance('*', 0, function (err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined') {
            console.log('Last Balance:' + lastbalance);
            var lastbalance = balance; // here you create a local lastbalance.
                                       // remove the var keyword to refer to
                                       // the original lastbalance
            updateCauses();
        }
        console.log('Balance:', balance);
        if (typeof callback == 'function') callback();

    });
};

setInterval(getBalance, 2000, lastbalance);

, :

var lastbalance;
getBalance = function (/*lastbalance, */callback) { // remove parameter
    btcclient.getBalance('*', 0, function (err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined') {
            console.log('Last Balance:' + lastbalance);
            /*var*/ lastbalance = balance; // remove var
            updateCauses();
        }
        console.log('Balance:', balance);
        if (typeof callback == 'function') callback();
    });
};

setInterval(getBalance, 2000/*, lastbalance*/); // remove argument
+4

lastBalance, !

  • var lastBalance =
  • getBalance ( )
  • var lastBalance =

, lastBalance = , . . undefined , .

, . - :

var lastbalance;
getBalance = function (callback){
  btcclient.getBalance('*', 0, function(err, balance) {
    if (err) return console.log(err);
    if (lastbalance != balance || typeof lastbalance == 'undefined'){
      console.log('Last Balance:' + lastbalance);
      lastbalance = balance;
      updateCauses();
    }
    console.log('Balance:', balance);

    if (typeof callback=='function') callback();
  });
};

setInterval(getBalance, 2000);

, API, , , .

+1

, 3 lastbalance, javascript .

var lastbalance;  
//creating a lastbalance var in the global scope (a member of the window object)

getBalance = function (lastbalance, callback){ 
    // <- create a function with a parameter of lastbalance. 
    // this one is a member of this getBalance function 
    // (functions in javascript are first class citezens i.e. 
    // objects too) and is different the the memeber of the 
    // window object called lastbalance.

    // that is  -- window['lastBalance'] != window.getBalance['lastBalance']; 

    var lastbalance = something   
    // create ANOTHER parameter lastbalance this time a member of the      
    // current code block, and not the same as the window member or
    // the function memeber. 

      // that is window['lastBalance'] 
      // != window.getBalance['lastBalance']
      // != window.getBalance.body['lastBalance']
};

, , .

//Create a global value. 
var lastBalance; 
getBalance = function (callback){
//this function doesnt need to be passed a variable it can read from the global scope. 
    btcclient.getBalance('*', 0, function(err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined'){
            console.log('Last Balance:' + lastbalance);
            lastbalance = balance;  // JS will work its way back and the only last balance is in the 
                                    // window object, so it will use that, no need to pass it a 
                                    // reference, just address it globablly. 

            updateCauses();
         }
         console.log('Balance:', balance);
         if (typeof callback=='function') callback();
    });
};

setInterval(getBalance, 2000);
+1
source

All Articles