英文:
How to retrieve a value from a variable outside of a condition?
问题
我正在学习JS,但我不知道是否能实现我想要的。
我有一个名为 btcVariationTotal
的变量,它位于一个条件中,我想在另一个名为 tmp
的变量中检索此变量的值,但此变量未包含在条件中。
我的问题是 tmp
总是显示为0。我不明白为什么?请问我该如何解决这个问题?
console.clear();
let wsBtc = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt@trade');
let btcStockPriceElement1 = document.getElementById('btcValue1');
let btcStockPriceElement2 = document.getElementById('btcValue2');
let btcLastPrice = null;
let btcStockObject = null;
wsBtc.onmessage = (event) => {
btcStockObject = JSON.parse(event.data);
};
let btc1 = 0, btc2 = 0;
let btcVariation_1_2 = 0;
let btcVariationTotal = 0;
let tmp = 0;
let btcRunTimers = setInterval(() => {
let minutes = new Date().getMinutes();
if (minutes === 51) {
let val1 = parseFloat(btcStockObject.p).toFixed(1);
let price = parseFloat(btcStockObject.p).toFixed(1);
btcStockPriceElement1.innerText = price;
btcStockPriceElement1.style.color =
!btcLastPrice || btcLastPrice === price
? 'black'
: price > btcLastPrice
? '#AAFF00'
: 'red';
btcLastPrice = price;
btcStockObject = null;
btc1 = val1;
}
if (minutes === 52) {
let val2 = parseFloat(btcStockObject.p).toFixed(1);
let price = parseFloat(btcStockObject.p).toFixed(1);
btcStockPriceElement2.innerText = price;
btcStockPriceElement2.style.color =
!btcLastPrice || btcLastPrice === price
? 'black'
: price > btcLastPrice
? '#AAFF00'
: 'red';
btcLastPrice = price;
btcStockObject = null;
btc2 = val2;
btcVariation_1_2 = ((parseFloat(btc2) - parseFloat(btc1)) / btc1 * 100);
document.getElementById("btcResult1").innerHTML = btcVariation_1_2.toFixed(2);
}
btcVariationTotal = parseFloat(btcVariation_1_2);
console.log("btc variation => " + btcVariationTotal);
document.getElementById("result").innerHTML = btcVariationTotal.toFixed(2);
tmp = btcVariationTotal;
}, 60000);
console.log("tmp => " + tmp);
注意:这段代码将 tmp
设置为 btcVariationTotal
的值,但由于 setInterval
是异步的,所以你在最后一行 console.log
中看到的值可能会是之前的值,因为 tmp
的更新需要等待 setInterval
的下一个周期。
英文:
I'm learning JS, but I don't know if it's possible to do what I want to achieve.
I have a variable named btcVariationTotal
which is in a condition, and I want to retrieve the value of this variable in another variable called tmp
, but this variable is not included in the condition.
My problem is that tmp
always shows me 0. I don't understand why? And how can I solve this problem, please?
I really want to retrieve the value outside the condition.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
console.clear();
let wsBtc = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt@trade');
let btcStockPriceElement1 = document.getElementById('btcValue1');
let btcStockPriceElement2 = document.getElementById('btcValue2');
let btcLastPrice = null;
let btcStockObject = null;
wsBtc.onmessage = (event) => {
btcStockObject = JSON.parse(event.data);
};
let btc1 = 0, btc2 = 0;
let btcVariation_1_2 = 0;
let btcVariationTotal = 0;
let tmp = 0;
let btcRunTimers = setInterval(() => {
let minutes = new Date().getMinutes();
if (minutes === 51) {
let val1 = parseFloat(btcStockObject.p).toFixed(1);
let price = parseFloat(btcStockObject.p).toFixed(1);
btcStockPriceElement1.innerText = price;
btcStockPriceElement1.style.color =
!btcLastPrice || btcLastPrice === price
? 'black'
: price > btcLastPrice
? '#AAFF00'
: 'red';
btcLastPrice = price;
btcStockObject = null;
btc1 = val1;
}
if (minutes === 52) {
let val2 = parseFloat(btcStockObject.p).toFixed(1);
let price = parseFloat(btcStockObject.p).toFixed(1);
btcStockPriceElement2.innerText = price;
btcStockPriceElement2.style.color =
!btcLastPrice || btcLastPrice === price
? 'black'
: price > btcLastPrice
? '#AAFF00'
: 'red';
btcLastPrice = price;
btcStockObject = null;
btc2 = val2;
btcVariation_1_2 = ( (parseFloat(btc2) - parseFloat(btc1)) / btc1 * 100);
document.getElementById("btcResult1").innerHTML = btcVariation_1_2.toFixed(2);
}
btcVariationTotal = (parseFloat(btcVariation_1_2));
console.log("btc variation => " + btcVariationTotal);
document.getElementById("result").innerHTML = btcVariationTotal.toFixed(2);
tmp = btcVariationTotal;
}, 60000);
console.log("tmp => " + tmp);
<!-- end snippet -->
答案1
得分: 2
好消息是,实际上你正在做你想做的事情:你正在检索btcVariationTotal
变量的值,并将其存储在在setInterval
回调之外定义的外部作用域中的tmp
中。
你唯一的问题是你不能显示修改后的tmp
,这是因为你只在设置tmp
之前调用console.log
,在设置tmp
之后从未调用它。用户Ivar已经在评论中尝试解释这一点,也许我可以详细说明一下:
在t=0时,你设置了tmp = 0
,然后使用setInterval
设置了定时器,关联了一个回调函数(此时不会运行),然后调用console.log
来显示tmp(它是0,因为没有回调运行过)。
在t=60秒时,你的回调函数运行,将btcVariationTotal
设置为某个值,并将其分配给tmp
。没有尝试显示tmp
的值。然后每隔60秒重复一次这个过程。
所以缺少的是你编写一些代码来在tmp
被更改后显示它的值。一种方法是将该代码放在其他回调函数中,并安排调用它。我建议使用一个简单的按钮。在你的HTML页面的某个地方添加以下内容:
<button id="show-tmp">Show tmp</button>
在你的JS代码的末尾添加以下行:
let btn = document.getElementById('show-tmp');
btn.onclick = function() {
console.log(`tmp: ${tmp}`);
}
现在点击按钮将显示tmp
内部的值;如果在第一个60秒之前这样做,它将显示0;如果在之后这样做,它将显示btcVariationTotal
中的任何值。
英文:
The good news is that you are in fact doing what you want: you are retrieving
the value of the btcVariationTotal
variable, and storing in tmp
, which is
defined in the outer scope, outside of your setInterval
callback.
The only problem you have is that you can't display a modified tmp
, and
that's because you only call console.log
before setting tmp
, you never
call it after it has been changed. User Ivar has tried to explain that in
the comments, maybe I can detail it a bit more:
At time t=0, you set tmp = 0
, you set your timers with setInterval
, associating
a callback function (which does NOT run at this point), and then you call
console.log
to display tmp (it's 0, because no callback has ever run).
At time t=60s, your callback runs, sets btcVariationTotal
to some value, and
assigns that to tmp
. No attempt is made to display the tmp
value. Then this
gets repeated every 60s.
So what's missing is for you to write some code that displays the tmp
value
after it has been changed. One way to do that, is to put that code inside
some other callback and arrange for it to be called. I suggest a simple
button. Add the following somewhere in your html page:
<button id="show-tmp">Show tmp</button>
Add the following lines at the end of your JS code:
let btn = document.getElementById('show-tmp');
btn.onclick = function() {
console.log(`tmp: ${tmp}`);
}
Now clicking on the button will show you the value inside tmp
; if you do it
before the first 60 seconds, it will show 0; if you do it afterwards, it will
show whatever value was in btcVariationTotal
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论