为什么在没有将其重新声明为局部变量的函数内不更新全局变量?

Why is a global variable not being updated inside a function that is NOT redeclaring it as a local variable?

在有人问我是否复习了其他问题之前,我确实找到了这个(link 段落下方)并更新了我的一个变量,因为它被重新声明为 local variable。它现在应该是 hoisted 作为具有优先权的 global

我主要是想为加密货币挖矿网站获取下一次付款时间。目前,我可以获取 上次付款时间 付款间隔 。数学不是问题...我 运行 遇到的问题是,当我抓住 variables(从 function 内部时,他们没有更新 global 一个.

原始声明@Global

<script>
        let pendingPaymentTrigger = false;
        let timeInterval = 0;
        let pendingPayment = 0;
 /** Script Below **/

然后我们进入 function,由于其中大部分与重现问题无关,我正在编辑它。

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

此时,console显示'1小时'||如果我从 pendingPayments 更改为 pendingPaymentTrigger 它显示 'True'

函数结束后...

}
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.

这个console显示'0'(原来的global variable)。

我猜这与 hoisting 有关,但我无法弄清楚问题出在哪里..


来自评论 1 的测试理论 - 移至函数末尾:

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

这并没有改变结果 - 这里有时间,但之后没有。

您可以在脚本之后声明一个函数,并在完成 XHR 请求并且更改变量后调用该函数。

/* ... */
pendingPaymentTrigger = true;
pendingPayment = Math.round((obj.body.primary.payments.balances + obj.body.primary.payments.generate + obj.body.primary.payments.immature) * 100) / 100;
doSomething();
/* ... */
function doSomething() {
    console.log(pendingPayment);
}