这个脚本的成功率如此之低

时间:2014-02-22 22:53:37

标签: javascript error-suppression

脚本本身运行正常;它没有错误。然而,在运行7,500次之后,只有35次成功。我如何提高Regex的成功率?因为现在,它没有做好这项工作。

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)

2 个答案:

答案 0 :(得分:2)

.get是异步的。这意味着在返回响应之前不会执行回调函数。到那时,你不再处于循环中,事实上循环已经完成。

虽然这是一个稍微不同的问题,this Question是一个非常好的阅读,以了解异步调用如何工作。


此外,即使没有异步行为,continue仍然是非法的,因为该函数具有自己的上下文,并且与循环不在同一上下文中。这个简单的例子也是非法的,可能更容易理解上下文问题:

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

function someFunc(){
    continue;  // illegal
}

虽然在您的情况下该函数是匿名函数,但同样的概念适用。

答案 1 :(得分:0)

请在代码中使用分号。

continue;

适用于循环,不适用于函数。使用

return;

关键字以脱离函数的上下文。