5

lastbalance永远不会得到定义,因此函数 getBalance 永远不会做它应该做的事情。我认为这可能是异步性的问题,但这只可能是第一个间隔之前的情况,对吧?有任何想法吗?

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

3 回答 3

5

两个问题。

1.:您定义lastbalance为函数参数...它在lastbalance您的函数上下文中创建了另一个变量...取代了在外部范围中声明的变量。

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.:你曾经在你的函数var中声明另一个。lastbalance不要那样做;它导致了上述相同的问题。

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
于 2013-11-06T19:18:44.543 回答
1

你有这么多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,但它可能并没有太大的错误。

于 2013-11-06T19:25:28.897 回答
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);
于 2013-11-06T19:29:10.627 回答