首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JS中从API获取数据

在JS中从API获取数据
EN

Stack Overflow用户
提问于 2019-12-23 08:00:06
回答 1查看 135关注 0票数 1

我最近一直在为我玩的一款游戏开发boxrater,我遇到了一个小问题。该函数从myTextarea获取用户输入值,然后将其拆分成一个数组。然后,该函数获取带有用户输入的api,如果成功,则将评级添加到小计类别(例如:黄金)。循环完成后,将为用户显示所有类别的值。

由于某些原因,合计和小计类别的输出总是具有不同的值。只有当用户输入很大时,才会发生这种情况。

Input that causes different outputs Output1 Output2

同样,这些输出完全是随机的,只是示例。有时它甚至输出正确的量,但很少见。

代码语言:javascript
复制
function boxRater() {
    var x = document.getElementById("myTextarea").value.split(/\n|-|Level|:| |,/);
    var helper = {
        total: "Total: ",
        shadow: 0,
        cursed: 0,
        rainbow: 0,
        glitter: 0,
        golden: 0,
        luminous: 0,
        amount: 0
    };
    var doNothing = {
        total: "Total: ",
        amount: 0
    };

    url = 'https://pokemoncreed.net/ajax/pokedex.php?pokemon='

    for (var i = 0; i <= x.length; i++) {
        fetch(url + x[i])
            .then((res) => res.json())
            .then((data) => {
                console.log(data.rating);
                if (data.success === false) {
                    console.log("error");
                    console.error(err);
                }
                if (data.name.includes("Golden")) {
                    if (data.rating.includes("m")) {
                        doNothing.amount = parseFloat(data.rating) * 1000000;
                        helper.golden = doNothing.amount + helper.golden;
                    }
                    if (data.rating.includes("k")) {
                        doNothing.amount = parseFloat(data.rating) * 1000;
                        helper.golden = doNothing.amount + helper.golden;
                    }
                }
                if (data.name.includes("Rainbow")) {
                    if (data.rating.includes("m")) {
                        doNothing.amount = parseFloat(data.rating) * 1000000;
                        helper.rainbow = doNothing.amount + helper.rainbow;
                    }
                    if (data.rating.includes("k")) {
                        doNothing.amount = parseFloat(data.rating) * 1000;
                        helper.rainbow = doNothing.amount + helper.rainbow;
                    }
                }
                if (data.name.includes("Shadow")) {
                    if (data.rating.includes("m")) {
                        doNothing.amount = parseFloat(data.rating) * 1000000;
                        helper.shadow = doNothing.amount + helper.shadow;
                    }
                    if (data.rating.includes("k")) {
                        doNothing.amount = parseFloat(data.rating) * 1000;
                        helper.shadow = doNothing.amount + helper.shadow;
                    }
                }
                if (data.name.includes("Luminous")) {
                    if (data.rating.includes("m")) {
                        doNothing.amount = parseFloat(data.rating) * 1000000;
                        helper.luminous = doNothing.amount + helper.luminous;
                    }
                    if (data.rating.includes("k")) {
                        doNothing.amount = parseFloat(data.rating) * 1000;
                        helper.luminous = doNothing.amount + helper.luminous;
                    }
                }
                if (data.name.includes("Cursed")) {
                    if (data.rating.includes("m")) {
                        doNothing.amount = parseFloat(data.rating) * 1000000;
                        helper.cursed = doNothing.amount + helper.cursed;
                    }
                    if (data.rating.includes("k")) {
                        doNothing.amount = parseFloat(data.rating) * 1000;
                        helper.cursed = doNothing.amount + helper.cursed;
                    }
                }
                if (data.name.includes("Glitter")) {
                    if (data.rating.includes("m")) {
                        doNothing.amount = parseFloat(data.rating) * 1000000;
                        helper.glitter = doNothing.amount + helper.glitter;
                    }
                    if (data.rating.includes("k")) {
                        doNothing.amount = parseFloat(data.rating) * 1000;
                        helper.glitter = doNothing.amount + helper.glitter;
                    }
                }

                helper.amount = helper.cursed + helper.rainbow + helper.golden + helper.luminous + helper.shadow + helper.glitter;
                document.getElementById("testing").innerHTML = x;

                document.getElementById("golden").innerHTML = helper.golden;
                document.getElementById("rainbow").innerHTML = helper.rainbow;
                document.getElementById("shadow").innerHTML = helper.shadow;
                document.getElementById("cursed").innerHTML = helper.cursed;
                document.getElementById("glitter").innerHTML = helper.glitter;
                document.getElementById("luminous").innerHTML = helper.luminous;
                document.getElementById("total").innerHTML = helper.amount;
            })
    }
}

下面的代码是我一直在测试它的html页面。同样,myTextarea中给出的这些示例也是有效的,但给出了一个更长的列表,如屏幕截图中所示。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<title> In update </title>
</head>
<body> Testing
<h1>Testing</h1>
<script src="js/boxrater.js"> </script>
   <div id="content">
     <header>
           <h2> Box Rater:</h2><br>
            <textarea rows ="6" cols="40" id="myTextarea">  *****HOW TO USE******
GoldenMankey  - Level 5
ShadowPichu - Level 5
RainbowCaterpie  - Level 5
GlitterDialga - Level 5
CursedMaractus - Level 5
LuminousBidoof - Level 5
*****Make sure to capitalize correctly (ex:GoldenMankey) and do the format above preferabaly ending each line with enter.*****
            </textarea>
            </header>
          <p>Read inside the box to understand how to use and formating.</p>
                    <p>Make sure to  to use <a  href="https://pokemoncreed.net/box_organise.php" target="_blank">Box Organise</a> for quick and easy use. </p>
                    <p> NOTE:The box rater doesn't calculate off for unbased pokemon or do 3x rate for rare genders </p>
       <button type="button" onclick="boxRater()">Rate Box</button>
                        <p id="rainbow"> Rainbow: </p>
            <p id="glitter"> Glitter: </p>
            <p id="cursed"> Cursed: </p>
            <p id="shadow">Shadow: </p>
            <p id="luminous">Luminous: </p>
            <p id="golden">Golden: </p>
            <p id="total">Total: </p>
            <p id="testing"> </p>

</div>
 </body>
</html>

如果有人知道如何解决这个问题,请给我解释一下。该评分器适用于个别情况,但我需要它的工作,为大型用户输入。~节日快乐!

EN

回答 1

Stack Overflow用户

发布于 2019-12-29 11:12:41

多亏了Jaromanda X注释,我能够修复我的代码。问题是因为它不是异步函数,所以我调整了代码,使其与异步函数一起工作。

代码语言:javascript
复制
function boxRater(){
var x = document.getElementById("myTextarea").value.split(/\n|-|Level|:| |5|,/);
return x;
}
url = 'https://pokemoncreed.net/ajax/pokedex.php?pokemon='
async function totalC () {
const x = boxRater();
console.log(x);
let helper = {total: "Total: ", shadow:0, cursed:0, rainbow:0, glitter:0,
golden:0,luminous:0, amount:0};
let doNothing = {total: "Total: ", amount:0};

for(let i =0; i <= x.length; i++){
let response = await fetch (url+x[i])
        let data = response.json()
        .then((data) => {
            console.log(data.rating);
        if(data.success === false){
    throw new SyntaxError("Error");
 }
if(data.success === true){

  if(data.name.includes("Golden")){
    if (data.rating.includes("m")){
      doNothing.amount = parseFloat(data.rating) * 1000000;
      helper.golden = doNothing.amount + helper.golden;
    }
    if (data.rating.includes("k")){
      doNothing.amount = parseFloat(data.rating) * 1000;
      helper.golden = doNothing.amount + helper.golden;
    }
}
if(data.name.includes("Rainbow")){
  if (data.rating.includes("m")){
      doNothing.amount = parseFloat(data.rating) * 1000000;
      helper.rainbow = doNothing.amount + helper.rainbow;
  }
  if (data.rating.includes("k")){
      doNothing.amount = parseFloat(data.rating) * 1000;
      helper.rainbow = doNothing.amount + helper.rainbow;
  }
}
if(data.name.includes("Shadow")){
  if (data.rating.includes("m")){
      doNothing.amount = parseFloat(data.rating) * 1000000;
      helper.shadow = doNothing.amount + helper.shadow;
  }
  if (data.rating.includes("k")){
     doNothing.amount = parseFloat(data.rating) * 1000;
      helper.shadow = doNothing.amount + helper.shadow;
  }
}
if(data.name.includes("Luminous")){
  if (data.rating.includes("m")){
      doNothing.amount = parseFloat(data.rating) * 1000000;
      helper.luminous = doNothing.amount + helper.luminous;
  }
  if (data.rating.includes("k")){
      doNothing.amount = parseFloat(data.rating) * 1000;
      helper.luminous = doNothing.amount + helper.luminous;
  }
}
if(data.name.includes("Cursed")){
    if (data.rating.includes("m")){
      doNothing.amount = parseFloat(data.rating) * 1000000;
      helper.cursed = doNothing.amount + helper.cursed;
    }
    if (data.rating.includes("k")){
      doNothing.amount = parseFloat(data.rating)*1000;
      helper.cursed = doNothing.amount + helper.cursed;
    }
}
if(data.name.includes("Glitter")){
  if (data.rating.includes("m")){
   doNothing.amount = parseFloat(data.rating) * 1000000;
      helper.glitter = doNothing.amount + helper.glitter;
  }
  if (data.rating.includes("k")){
      doNothing.amount = parseFloat(data.rating) * 1000;
      helper.glitter = doNothing.amount + helper.glitter;
  }
}
if (data.rating.includes("Not rated")){
    console.log("Not rated");
}
} 
helper.amount = helper.cursed+helper.rainbow+helper.golden+helper.luminous+helper.shadow+helper.glitter;
document.getElementById("golden").innerHTML = helper.golden;
document.getElementById("rainbow").innerHTML = helper.rainbow;
document.getElementById("shadow").innerHTML = helper.shadow;
document.getElementById("cursed").innerHTML = helper.cursed;
document.getElementById("glitter").innerHTML = helper.glitter;
document.getElementById("luminous").innerHTML = helper.luminous;
document.getElementById("cost").innerHTML = helper.amount ;
        })

}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59448958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档