2015-05-03 27 views
0

我试图在JSON数据的每个成员上运行一个函数。它只是在最后一部分运行它。代码很混乱,因为我一直在添加和删除几个小时的调试。我是小白和JS :(只有通过JSON数据的最后部分时才会通过函数

这是当将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(); } 
    }); 
} 

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

$.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

一旦当将CheckIt内对所有的console.log帽子的名字,它只能输出一个ID,叫做蒸汽朋克的Tricorn两倍的JSON列表 最后一个ID,这需要更换的两个帽子实际上应该在那里。

只追加蒸汽朋克Tricorn。

蒸汽朋克的Tricorn甚至不应该if语句,说

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

基本上它切换是应该的东西,是不是在那里的一切内部得到(蒸汽朋克的Tricorn)

对不起,如果这是超级混乱,我尽我所能解释它。

回答

0

我觉得你的问题是the variable is not behaving as you are expecting。 (参见: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]); 
    } 
});