0

I'm trying to run a function on every member of JSON data. It only runs it on the last part of that though. The code is messy because I've been adding and removing debugging for hours. I'm a noob and JS :(

This is the checkIt function code: (the checkBlacklist.php returns either a true or 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(); }
  });
 }

And here is the code that goes through the JSON at 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); }
});

} 
});

Okay, so what it does is:

Output:

Outputs all the hat names on the console.log before checkIt

Once inside the checkIt, it outputs only one ID, the last ID in the JSON list called Steampunk Tricorn TWICE, which takes the replacement of the two hats that are actually supposed to be there.

Appends the Steampunk Tricorn only.

The Steampunk Tricorn shouldn't even get inside of the if statement that says

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

Basically it switches everything that is supposed to be there with something that is not (Steampunk Tricorn)

Sorry if that was SUPER confusing, I tried my best to explain it.

Karten
  • 13
  • 6

1 Answers1

0

I think your problem is that the variable is not behaving as you are expecting. (Also see: https://stackoverflow.com/a/2853627/1938640)

This fix might work:

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]);
    }
});
Community
  • 1
  • 1
ZombieSpy
  • 1,376
  • 12
  • 23