为什么我的variables还没有定义?

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

两个问题。

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

 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 

你有这么多lastBalancevariables敲门!

  • 一个在外部范围var lastBalance =
  • 一个在getBalance函数中(一个参数名)
  • 一个在callback函数var lastBalance =

这意味着当你在内部函数中执行lastBalance =时,只有第三个variables被设置。 没有任何代码在外部范围内设置variables。 内部作用域中的一个被设置为每次运行该function时都undefined

我很确定你只想要一个variables。 你需要做这样的事情:

 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,但可能没有太大的错误。

所以在这里你有3个名为lastbalance的variables,而javascript将使用范围最窄的variables。

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