0

The script itself works fine and all; it is errorless. However, out of running 7,500 times, only 35 were successful. How would I improve the success rate of Regex? Because right now, it is not doing the job.

var IDs = [136758649, 116770724, 136171998]//A lot more IDS than this
var PriceWanting = 60
var scanneditems = 0
var itemerror = 0
document.write('<p id = "title">Total number bought: 0 items for a total of 0</p>')
document.write('<p id = "scanned">Items scanned: 0</p>')
document.write('<p id = "itemerrors">Items scanned: 0</p>')
var buys = 0
var totalrobuxspent = 0
console.log("Bot started")
var loop = setInterval(function()
{
  for (var i = 0;i<IDs.length;i++) {
  $.get(" http://m.roblox.com/items/" + IDs[i] + "/privatesales",function(data) {
      var Regex = /\<span class="currency-robux">([\d,]+)\<\/span\>/;
      var PriceSelling = data.match(Regex);
      scanneditems = scanneditems + 1
      document.getElementById("scanned").innerHTML = "Scanned items: " + scanneditems
      PriceSelling = PriceSelling ? PriceSelling[1] : '';
      if (PriceSelling.length < 1) {
        itemerrors = itemerrors + 1
        document.getElementById(''itemserror'').innerHTML = ''Total errors: '' + itemerrors
        return
      }
      PriceSelling = Number(PriceSelling.replace(",",""))
      PriceSelling = PriceSelling * 1
      totalrobuxspent = totalrobuxspent + PriceSelling
      var remaining = PriceWanting - PriceSelling
      if (remaining >= -0.1) 
      {
        buys = buys + 1
        document.getElementById("title").innerHTML = "Total number of items bought: " + buys + " for a total of " + totalrobuxspent + " "
var Regex2 = /<a href="\/Catalog\/VerifyTransfer\DuserAssetOptionId=([\d,]+)\Damp;expectedPrice=([\d,]+)">/
                                var HatBuyId = data.match(Regex2)[1]
                                var HatBuyLink = "http://m.roblox.com/Catalog/VerifyPurchase?assetid=" + HatBuyId + " &type=robux&expectedPrice=" + PriceSelling
var Explorer = document.createElement('iframe');
                                function Buy(){
       Explorer.contentDocument.forms[0].submit();
       console.log("Item purchase complete, scanning again.")
       var inf = document.createElement('div');
       inf.style.fontSize = "18px";
       inf.style.background = "rgba(0,0,5,0)";
       inf.style.position = "absolute";
       inf.style.width = "100%";
       inf.style.height = "18pt";
       inf.innerText = "Bot currently running. Purchases: "+answer;
       document.body.appendChild(inf);
                                };
                                Explorer.onload = Buy;
                                Explorer.width = "100%";
                                Explorer.height = "85%";
                                Explorer.src = HatBuyLink;
                                document.body.innerHTML = "";
                                document.body.appendChild(Explorer);
    }
  })
}
},500)
OneChillDude
  • 7,250
  • 9
  • 36
  • 76

2 Answers2

2

.get is asynchronous. This means that the callback function is not executed until the response is returned. By that point, you are no longer in a loop and in fact the loop has already completed.

Though it's about a slightly different problem, this Question is a very good read to understand how asynchronous calls work.


In addition, continue here would still be illegal, even without the asynchronous behavior, because the function has its own context and is not in the same context as the loop. This simple example would also be illegal and may be easier to understand the context issue:

for(var i=0; i<10; i++){
    someFunc();
}

function someFunc(){
    continue;  // illegal
}

Though in your case the function is an anonymous function, the same concept applies.

Community
  • 1
  • 1
James Montagne
  • 73,502
  • 13
  • 107
  • 127
  • It's more a question of context than order of execution, I believe, but this is largely correct. – Jimmy Sawczuk Feb 22 '14 at 23:09
  • 1
    This answer is correct; the OP could use `return;` instead of `continue;` to achieve the same result though; terminating that ajax handler due to bad data. – Collin Grady Feb 22 '14 at 23:11
  • 1
    @JimmySawczuk Technically, yes. But in terms of understanding, the OP clearly thinks that these callbacks are being called in sequence, hence the belief that continue will move on to the next iteration of the loop. Let me reword a bit. – James Montagne Feb 22 '14 at 23:12
0

Please, use semicolons in your code.

continue;

is applicable in loops, not in functions. Use the

return;

keyword to get out of the context of a function.

Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148