0

我正在尝试对 JSON 数据的每个成员运行一个函数。它只在最后一部分运行它。代码很混乱,因为我已经添加和删除了几个小时的调试。我是菜鸟和 JS :(

这是 checkIt 函数代码:(checkBlacklist.php 返回真或假)

 var nope = "0";
function checkIt(id, _callback) {
$.post("checkBlacklist.php?id=" + id, function(data) {
          console.log("posted for " + id + " with " + data);
     if (data == "false") {
        nope = 1;
        _callback();
     } else { nope = 2; _callback(); }
  });
 }

这是在http://robloxplus.com:2052/inventory?username=iTracking上通过 JSON 的代码

$.get("http://robloxplus.com:2052/inventory?username=iTracking").success(function(r){
for(var id in r.data.hat.data){
    $( "h2" ).empty(); // remove the "Items Loading" title
if (r.data.hat.data[id].rap > 1000 && r.data.hat.data[id].rap < 5000) { 
// HERE IS WHERE IT MESSES UP, IT'S REALLY WEIRD. 
console.log(r.data.hat.data[id].name);
var selectedNope = 0; 
checkIt(r.data.hat.data[id].id, function() {
    selectedNope = nope;
    nope = 0;
    console.log(selectedNope + " for " + r.data.hat.data[id].name);
        if (selectedNope == 1) {
            $("body").append("<center><div class='itemContainer_blue'><div class='itemName'><a href='http://www.roblox.com/---item?id=" + r.data.hat.data[id].id + "'>" +   r.data.hat.data[id].name + "</a></div><div class='itemRap'>RAP: " + r.data.hat.data[id].rap + "</div><div class='itemImage'> <a href='#' title='Item Name'> <center><img src='" + r.data.hat.data[id].image + "' alt='itemImage'/></center></a></div> <div class='itemPrice'>Manual Price Set</div></div></center>");
        } else { console.log("it was not 1, it was " + selectedNope + " for " +  r.data.hat.data[id].name); }
});

} 
});

好的,所以它的作用是:

输出:

在 checkIt 之前输出 console.log 上的所有帽子名称

一旦进入 checkIt,它只输出一个 ID,即 JSON 列表中的最后一个 ID,称为 Steampunk Tricorn TWICE,它替换了实际应该存在的两顶帽子。

仅附加蒸汽朋克三角。

Steampunk Tricorn 甚至不应该进入 if 语句

if (r.data.hat.data[id].rap > 1000 && r.data.hat.data[id].rap < 5000) { 

基本上它将所有应该存在的东西与不存在的东西交换(蒸汽朋克三角)

对不起,如果那是超级混乱,我尽力解释它。

4

1 回答 1

0

我认为您的问题是该变量的行为不符合您的预期。(另见:https ://stackoverflow.com/a/2853627/1938640 )

此修复程序可能有效:

function checkHat(hat) {
    if (hat.rap > 1000 && hat.rap < 5000) {
        $.post("checkBlacklist.php?id=" + hat.id, function (data) {
            console.log(data + " for " + hat.name);
            if (data == "false") {
                $("body").append("<center><div class='itemContainer_blue'><div class='itemName'><a href='http://www.roblox.com/---item?id=" + hat.id + "'>" + hat.name + "</a></div><div class='itemRap'>RAP: " + hat.rap + "</div><div class='itemImage'> <a href='#' title='Item Name'> <center><img src='" + hat.image + "' alt='itemImage'/></center></a></div> <div class='itemPrice'>Manual Price Set</div></div></center>");
            } else {
                console.log("it was not 1, it was " + data + " for " + hat.name);
            }
        });
    }
}

$.get("http://robloxplus.com:2052/inventory?username=iTracking").success(function (r) {
    $("h2").empty();
    for (var id in r.data.hat.data) {
        checkHat(r.data.hat.data[id]);
    }
});
于 2015-06-09T14:56:11.153 回答