最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my variable still undefined? - Stack Overflow

programmeradmin1浏览0评论

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

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);
Share Improve this question asked Nov 6, 2013 at 19:17 Kinnard HockenhullKinnard Hockenhull 3,0206 gold badges31 silver badges34 bronze badges 1
  • The isssue is you have a global variable called last balance on the top, and then you have another local variable called last balance, you need to understand the scope of each variable you are working with. In this case your local variable inside the get balance will get created and updated and your global one will never be declared. – Marko Commented Nov 6, 2013 at 19:23
Add a ment  | 

3 Answers 3

Reset to default 5

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

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.

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);
发布评论

评论列表(0)

  1. 暂无评论