【问题标题】:Such a low success rate with this script这个脚本的成功率这么低
【发布时间】:2014-03-24 13:54:32
【问题描述】:

脚本本身运行良好;它没有错误。然而,在运行 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)

【问题讨论】:

  • 请提供SSCCE,以便我们可视化示例。
  • 这里,我添加了主要部分。
  • return放在那里,天哪。 continue 到底是什么意思?
  • 我在一个巨大的脚本网站上读到它只会跳过 for 循环中的 i 东西。
  • 您不能在回调函数的主体中包含continue,因为continues 是for 循环,而不是函数。您可以使用return 跳过该功能的其余部分。

标签: javascript error-suppression


【解决方案1】:

请在您的代码中使用分号。

continue;

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

return;

离开函数上下文的关键字。

【讨论】:

    【解决方案2】:

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

    虽然这是一个稍微不同的问题,但this Question 是一本很好的读物,可以了解异步调用的工作原理。


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

    for(var i=0; i<10; i++){
        someFunc();
    }
    
    function someFunc(){
        continue;  // illegal
    }
    

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

    【讨论】:

    • 我相信这更多的是上下文问题而不是执行顺序,但这在很大程度上是正确的。
    • 这个答案是正确的; OP 可以使用 return; 而不是 continue; 来实现相同的结果;由于数据错误而终止该 ajax 处理程序。
    • @JimmySawczuk 从技术上讲,是的。但就理解而言,OP 清楚地认为这些回调是按顺序调用的,因此相信 continue 将继续进行循环的下一次迭代。让我重新措辞一下。
    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-04
    • 2011-08-26
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多