我正在尝试在JSON数据的每个成员上运行一个函数。它只在它的最后部分运行它。代码很乱,因为我一直在添加和删除调试几个小时。我是菜鸟和JS :(
这是checkIt函数代码:( checkBlacklist.php返回true或false)
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。
Steampunk Tricorn甚至不应该进入if语句
if (r.data.hat.data[id].rap > 1000 && r.data.hat.data[id].rap < 5000) {
基本上它会切换那些应该存在的东西(Steampunk Tricorn)
很抱歉,如果这太令人困惑,我会尽力解释。
答案 0 :(得分: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]);
}
});