简体   繁体   English

如何从条件之外的变量中检索值?

[英]How to retrieve a value from a variable outside of a condition?

I'm learning JS, but I don't know if it's possible to do what I want to achieve.我正在学习JS,但我不知道是否可以做我想达到的目标。

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.我有一个名为btcVariationTotal的变量,它处于一个条件中,我想在另一个名为tmp的变量中检索该变量的值,但该变量不包含在条件中。

My problem is that tmp always shows me 0. I don't understand why?我的问题是tmp总是显示 0。我不明白为什么? And how can I solve this problem, please?请问我该如何解决这个问题?

在此处输入图像描述

I really want to retrieve the value outside the condition.我真的很想检索条件之外的值。

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

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.好消息是您实际上正在做您想做的事情:您正在检索btcVariationTotal变量的值,并存储在tmp中,它在setInterval回调之外的外部 scope 中定义。

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.您遇到的唯一问题是您无法显示修改后的tmp ,那是因为您只在设置tmp之前调用console.log ,而在更改您永远不会调用它。 User Ivar has tried to explain that in the comments, maybe I can detail it a bit more:用户Ivar试图在评论中解释这一点,也许我可以更详细一点:

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).在时间 t=0,你设置tmp = 0 ,你用setInterval设置你的定时器,关联一个回调 function (此时不运行),然后你调用console.log来显示 tmp (它是 0,因为没有回调曾经运行过)。

At time t=60s, your callback runs, sets btcVariationTotal to some value, and assigns that to tmp .在时间 t=60s 时,您的回调运行,将btcVariationTotal设置为某个值,并将其分配给tmp No attempt is made to display the tmp value.不会尝试显示tmp值。 Then this gets repeated every 60s.然后每 60 秒重复一次。

So what's missing is for you to write some code that displays the tmp value after it has been changed.因此,您缺少的是编写一些代码来显示更改tmp值。 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:在您的 html 页面的某处添加以下内容:

<button id="show-tmp">Show tmp</button>

Add the following lines at the end of your JS code:在 JS 代码末尾添加以下行:

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 ;现在单击按钮将显示tmp中的值; if you do it before the first 60 seconds, it will show 0;如果您在前 60 秒之前执行此操作,它将显示 0; if you do it afterwards, it will show whatever value was in btcVariationTotal .如果你之后这样做,它会显示btcVariationTotal中的任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM