Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 函数与if-else不匹配';我不能正常工作_Javascript - Fatal编程技术网

Javascript 函数与if-else不匹配';我不能正常工作

Javascript 函数与if-else不匹配';我不能正常工作,javascript,Javascript,我做了一个函数,如果一个变量不大于或等于另一个变量,它应该禁用一个按钮。此函数在setInterval()上每秒运行一次,要比较的第一个变量在setInterval()上也会递增一。但是,函数(evitarNegs())工作不正常,并且该按钮始终处于禁用状态。抱歉,部分代码是西班牙语的 Javascript: var GmB = {cantidad: 0, perSec: 1}; function Upgrade (pb, ps) { this.precioBase = pb;

我做了一个函数,如果一个变量不大于或等于另一个变量,它应该禁用一个按钮。此函数在setInterval()上每秒运行一次,要比较的第一个变量在setInterval()上也会递增一。但是,函数(evitarNegs())工作不正常,并且该按钮始终处于禁用状态。抱歉,部分代码是西班牙语的

Javascript:

var GmB = {cantidad: 0, perSec: 1};

function Upgrade (pb, ps) {
    this.precioBase = pb;
    this.perSec = ps;
    this.cantidad = 0;
    this.precio = pb;
}

Upgrade.prototype.comprar = function() {
    GmB.cantidad = GmB.cantidad - this.precio;
    GmB.perSec = GmB.perSec + this.perSec;
    this.cantidad++;
    document.getElementById("gmb").innerHTML = GmB.cantidad;
    this.precio = Math.ceil(this.precioBase*Math.pow(1.15, this.cantidad));
    evitarNegs();
};

function loop() {
    GmB.cantidad = GmB.cantidad + GmB.perSec;
    document.getElementById("gmb").innerHTML = GmB.cantidad;
    evitarNegs();
}

var upg = new Upgrade(10, 1);
var boton1 = document.getElementById("boton1");
boton1.disabled = true;
window.setInterval(loop, 1000);

//Problematic function
function evitarNegs() {
    if (!(GmB >= upg.precio)) {
        boton1.disabled = true;
    }else {
        boton1.disabled = false;
    }
}

boton1.onclick = function() {
    upg.comprar();
};
<html>
    <head>
        <title>Gummy Bears</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">

    </head>
    <body>
        <p id="gmb">0</p>
        <button id="boton1" type="button">Upgrade 1</button>
        <script src="main.js"></script>
    </body>
</html>
HTML:

var GmB = {cantidad: 0, perSec: 1};

function Upgrade (pb, ps) {
    this.precioBase = pb;
    this.perSec = ps;
    this.cantidad = 0;
    this.precio = pb;
}

Upgrade.prototype.comprar = function() {
    GmB.cantidad = GmB.cantidad - this.precio;
    GmB.perSec = GmB.perSec + this.perSec;
    this.cantidad++;
    document.getElementById("gmb").innerHTML = GmB.cantidad;
    this.precio = Math.ceil(this.precioBase*Math.pow(1.15, this.cantidad));
    evitarNegs();
};

function loop() {
    GmB.cantidad = GmB.cantidad + GmB.perSec;
    document.getElementById("gmb").innerHTML = GmB.cantidad;
    evitarNegs();
}

var upg = new Upgrade(10, 1);
var boton1 = document.getElementById("boton1");
boton1.disabled = true;
window.setInterval(loop, 1000);

//Problematic function
function evitarNegs() {
    if (!(GmB >= upg.precio)) {
        boton1.disabled = true;
    }else {
        boton1.disabled = false;
    }
}

boton1.onclick = function() {
    upg.comprar();
};
<html>
    <head>
        <title>Gummy Bears</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">

    </head>
    <body>
        <p id="gmb">0</p>
        <button id="boton1" type="button">Upgrade 1</button>
        <script src="main.js"></script>
    </body>
</html>


粘熊

0

升级1
似乎您正在将对象与
GmB>=upg.precio
中的整数进行比较。您可能必须将其替换为
GmB.cantidad>=upg.precio

似乎您正在将对象与
GmB>=upg.precio
中的整数进行比较。您可能必须将其替换为
GmB.cantidad>=upg.precio
您正在将
GmB
upg.precio
进行比较,但
GmB
是一个对象。所以你想要

function evitarNegs() {
    if (!(GmB.cantidad >= upg.precio)) {
        boton1.disabled = true;
    } else {
        boton1.disabled = false;
    }
}
然而,这可以写得容易得多

function evitarNegs() {
    boton1.disabled = GmB.cantidad < upg.precio;
}
函数evitarNegs(){
boton1.disabled=GmB.cantidad

小提琴:

您正在将
GmB
upg.precio
进行比较,但
GmB
是一个对象。所以你想要

function evitarNegs() {
    if (!(GmB.cantidad >= upg.precio)) {
        boton1.disabled = true;
    } else {
        boton1.disabled = false;
    }
}
然而,这可以写得容易得多

function evitarNegs() {
    boton1.disabled = GmB.cantidad < upg.precio;
}
函数evitarNegs(){
boton1.disabled=GmB.cantidad
小提琴: