简体   繁体   English

如何使因变量在HTML / javascript中响应和更新?

[英]How can I make dependent variables respond and update in HTML/javascript?

I was hoping someone could help me out. 我希望有人可以帮助我。 I am modelling a (simple) nuclear reactor and have a number of variables which are dependent on other variables, and on user inputs. 我正在为一个(简单的)核反应堆建模,并具有许多变量,这些变量取决于其他变量和用户输入。

In the below code, initially the Control Rod Height is set at 50%, but the user can move the control rods up and down in the limits of 0 to 100. Reactor temperature is directly dependent on the control rod height, but I can't get it to update. 在下面的代码中,最初将控制杆高度设置为50%,但用户可以在0到100的范围内上下移动控制杆。反应堆温度直接取决于控制杆高度,但是我可以使其更新。

The TEST button displays the updated Control Rod Height, but the TEMPERATURE button will only ever display the initial value (based on control rod height = 50). TEST按钮显示更新的控制杆高度,但是TEMPERATURE按钮将仅显示初始值(基于控制杆高度= 50)。

Is it possible to make Reactor Temperature update as the user changes the height of the control rods? 用户改变控制棒的高度时是否可以使反应堆温度更新?

Thank you! 谢谢!

HTML 的HTML

<div id="game">
<button onClick = "IncCRH()">Increase Control Rod Height</button>
<button onClick = "DecCRH()">Decrease Control Rod Height</button>
<div id="result"></div>
<br>
<br>
<button onClick="test()">TEST</button>
<br>
<br>
<button onClick="ReacTemp()">TEMPERATURE</button>
</div>

Javascript Java脚本

var ControlRodHeight = 50;
var ReactorTemperature = (250 + (ControlRodHeight*20));

function IncCRH(){
    user = ++ControlRodHeight;
    if (ControlRodHeight > 100)
    ControlRodHeight = 100;
}
function DecCRH() {
    user = --ControlRodHeight;
    if (ControlRodHeight < 0)
    ControlRodHeight = 0
}

function test(){
    alert (ControlRodHeight);
}

function ReacTemp(){
    alert (ReactorTemperature);
}

You have to manually recalculate the ReactorTemperature when you change ControlRodHeight: 更改ControlRodHeight时,您必须手动重新计算ReactorTemperature:

function IncCRH(){
    user = ++ControlRodHeight;
    if (ControlRodHeight > 100) {
        ControlRodHeight = 100;
    }
    ReactorTemperature = (250 + (ControlRodHeight*20));
}

(Or call a function which is doing it, in order to not repeat the code.) (或调用正在执行此操作的函数,以便不重复代码。)

you need to change ReactorTemperature value in these functions to have updated value var ControlRodHeight = 50; 您需要在这些函数中将ReactorTemperature值更改为具有更新后的值var ControlRodHeight = 50; var ReactorTemperature = (250 + (ControlRodHeight*20)); var ReactorTemperature =(250 +(ControlRodHeight * 20));

function IncCRH(){
    user = ++ControlRodHeight;
    if (ControlRodHeight > 100){
    ControlRodHeight = 100;
}
    ReactorTemperature = (250 + (ControlRodHeight*20));
}
function DecCRH() {
    user = --ControlRodHeight;
    if (ControlRodHeight < 0){
    ControlRodHeight = 0
}
ReactorTemperature = (250 + (ControlRodHeight*20));
}

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

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