Before anyone asks if I reviewed other questions, I did find this one (link below paragraph) and updated one of my variables because it was redeclared as a local variable
. It should now be hoisted
as a global
with precedence.
Global variable in function not updating
I'm basically trying to get a Next Payment Time for a crypto-mining website. Currently, I'm able to grab the Last Payment Time and Payment Interval. The math isn't the issue... the problem I run in to is that when I grab the variables
(from inside a function
, they are not updating the global
one.
Original Declaration @ Global
<script>
let pendingPaymentTrigger = false;
let timeInterval = 0;
let pendingPayment = 0;
/** Script Below **/
Then we break into the function
, which I'm redacting due to most of it not being relevant to recreate the issue.
function values(baseAPI, minersAPI, activeMinersAPI, workersAPI) {
if (window.location.href.indexOf("address") > -1) {
const urlParams = new URLSearchParams(window.location.search);
const wallet = urlParams.get('address');
yourAPI = minersAPI + "?method=" + wallet;
$("#walletRequest").hide();
$("dashboardDisplay").show();
const XHR = new XMLHttpRequest();
XHR.open("GET", yourAPI, true);
XHR.onload = function() {
if (this.status === 200) {
obj = JSON.parse(this.responseText);
if (obj.body.primary?.payments?.balances === undefined || obj.body.primary?.payments?.generate === undefined) {
document.getElementById('pendingPayments').innerHTML = "Innactive";
} else {
document.getElementById('pendingPayments').innerHTML = Math.round((obj.body.primary.payments.balances + obj.body.primary.payments.generate + obj.body.primary.payments.immature) * 100) / 100 + " HVQ";
pendingPaymentTrigger = true;
pendingPayment = Math.round((obj.body.primary.payments.balances + obj.body.primary.payments.generate + obj.body.primary.payments.immature) * 100) / 100;
console.log(pendingPayment);
}
At this point, the console
shows '1 Hour' || If I change from pendingPayments
to pendingPaymentTrigger
it shows 'True'
Right after the function ends...
}
console.log(pendingPayment);
let displayBlocks = new Array();
let displayBlocks2 = new Array();
function checkBlocks() { // This is the next function - not relevant, but supporting that I am **out** of the last function.
This console
shows '0' (the original global variable
).
I'm guessing it has something to do with hoisting
but I can't figure out where the issue lies..
Tested theory from comments 1 - moving to the end of the function:
if (obj.body.primary?.payments?.balances === undefined || obj.body.primary?.payments?.generate === undefined) {
document.getElementById('pendingPayments').innerHTML = "Innactive";
} else {
document.getElementById('pendingPayments').innerHTML = Math.round((obj.body.primary.payments.balances + obj.body.primary.payments.generate + obj.body.primary.payments.immature) * 100) / 100 + " HVQ";
pendingPaymentTrigger = true;
pendingPayment = Math.round((obj.body.primary.payments.balances + obj.body.primary.payments.generate + obj.body.primary.payments.immature) * 100) / 100;
console.log(pendingPayment);
}
}
};
XHR.send();
This did not change the outcome - has time here, but not after.