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);
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!
var lastBalance =
getBalance
function (a parameter name)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With