Kinnard Hockenhull
Kinnard Hockenhull

Reputation: 3010

Why is my variable still undefined?

lastbalance never get's defined and because of that the function getBalance never does what its supposed to. I thought this might be an issue with asynchronicity but that could only be the case before 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);

Upvotes: 5

Views: 6869

Answers (3)

canon
canon

Reputation: 41675

Two issues.

1.: You defined lastbalance as a function parameter... which created another lastbalance variable in the context of your function... which superseded 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 var to declare yet another lastbalance in your function. Don't do that; it caused the same issue 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);

Finally, your code should look something like this:

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

Upvotes: 5

gbtimmon
gbtimmon

Reputation: 4322

So here you have 3 DIFFERENT variables named lastbalance and javascript is going to use the variable with the narrowest scope.

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']
};

What you want shoud look more like this.

//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);

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237865

You have so many lastBalance variables knocking around!

  • one in the outer scope var lastBalance =
  • one in the getBalance function (a parameter name)
  • one in the callback function var lastBalance =

This means that when you do lastBalance = in the inner function, only the third of the variables gets set. No code ever sets the variable in the outside scope. The one in the inner scope is set to undefined every time the function is run.

I'm pretty sure you only want the one variable. You need to do something like this:

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);

This may not be exactly right, as I am unfamiliar with your API, but it probably isn't far wrong.

Upvotes: 1

Related Questions