Javascript 超过最大堆栈大小-循环出现多个AJAX问题,且函数未启动

Javascript 超过最大堆栈大小-循环出现多个AJAX问题,且函数未启动,javascript,jquery,ajax,Javascript,Jquery,Ajax,我的部分函数没有执行,我知道代码很糟糕,我正在努力提高效率 Chrome开发者控制台错误: 未捕获范围错误:超过最大调用堆栈大小 我试着对我想要完成的事情进行评论。我希望这有帮助 我正在从一个网站上抓取数据(大量数据,大约6000个请求)。 脚本需要从中获取信息,然后确定比赛是否结束,如果没有,它只需将比赛信息记录到我的数据库中,然后继续 如果比赛还没有结束,我需要通过ajax从我的域中获取用户id,然后我将把信息保存到数据库中,然后继续下一条记录 我不得不使用自定义循环,因为ajax搞砸了订单

我的部分函数没有执行,我知道代码很糟糕,我正在努力提高效率

Chrome开发者控制台错误:

未捕获范围错误:超过最大调用堆栈大小

我试着对我想要完成的事情进行评论。我希望这有帮助

我正在从一个网站上抓取数据(大量数据,大约6000个请求)。 脚本需要从中获取信息,然后确定比赛是否结束,如果没有,它只需将比赛信息记录到我的数据库中,然后继续

如果比赛还没有结束,我需要通过ajax从我的域中获取用户id,然后我将把信息保存到数据库中,然后继续下一条记录

我不得不使用自定义循环,因为ajax搞砸了订单

这是我的代码,如果你需要更好的解释,请告诉我

function getTournamentInfo(){
  /** 
   *    We need to create a loop that fires AJAX only after the first request
   *    has finished so the results don't get out of order and screwed up. 
   *    Evaluate if the current tournamentid is greater than the ending id
   **/
  if (tournamentCurrentId <= tournamentEndId ) {
    // The current tournament id is less than the ending so we want to continue
    // with the funciton and get the tournament
    var data = $.getTournamentResults(tournamentCurrentId);
    //console.log(data);
    // Get the necessary information from the results and store them in their corresponding variables
    tournamentName = $.trim($(data).find('#tournament_container>h3').text());
    tournamentStatus = $.trim($(data).find('#tournament_container').clone().children().remove().end().text());
    tournamentBuyIn = $.trim($(data).find(".li_title:contains('Buy In')").parent().clone().children().remove().end().text());
    tournamentStart = $.trim($(data).find('.start').first().contents().filter(function() {return this.nodeType == 3;}).text());
    tournamentEnd = $.trim($(data).find('.finish').first().contents().filter(function() {return this.nodeType == 3;}).text());
    tournamentDuration = $(data).find('.duration2').first().contents().filter(function() {return this.nodeType == 3;}).text();
    tournamentPrizePool = $.trim($(data).find(".li_title:contains('Prize Pool')").parent().clone().children().remove().end().text());
    tournamentParticipants = $.trim($(data).find(".li_title:contains('Registered Players')").parent().clone().children().remove().end().text());


    // Process variables to required mySQL format
    //tournamentStart = convertTournamentTimes(tournamentStart);
    //tournamentEnd = convertTournamentTimes(tournamentEnd);
    tournamentBuyIn = removeDollarSigns(tournamentBuyIn);
    tournamentPrizePool = removeDollarSigns(tournamentPrizePool);

    // We only want to process results for Tournaments that are finsihed,
    // excluding those that are cancelled and pending.
    // Check the tournament status and determine proper action.
    if (tournamentStatus != "Finished"){
      // The tournament is either Cancelled or Pending just store in the information in the database.

      //Temporary .append() to be replaced by SQL insert function
      $('#results').append($("<div>").append("<span>" + tournamentCurrentId + " </span>").append("<span>" + tournamentName + " </span>").append("<span>" + tournamentStatus + " </span>").append("<span>" + tournamentBuyIn + " </span>").append("<span>" + tournamentStart + " </span>").append("<span>" + tournamentEnd + " </span>").append("<span>" + tournamentDuration + " </span>").append("<span>" + tournamentPrizePool + " </span>").append("<span>" + tournamentParticipants + " </span>"));


    } else {
      // The tournament is finished we need to get the player info

      //Temporary .append() to be replaced by SQL insert function
      $('#results').append($("<div>").append("<span>" + tournamentCurrentId + " </span>").append("<span>" + tournamentName + " </span>").append("<span>" + tournamentStatus + " </span>").append("<span>" + tournamentBuyIn + " </span>").append("<span>" + tournamentStart + " </span>").append("<span>" + tournamentEnd + " </span>").append("<span>" + tournamentDuration + " </span>").append("<span>" + tournamentPrizePool + " </span>").append("<span>" + tournamentParticipants + " </span>"));

      // Get player info

      /** Using old fuction to test see if it works 
       *
       *  Notes:
       *
       **/

       var columns = $(data).find('#ranked_players thead th').map(function() {
            return $(this).text().toLowerCase();
          });

          var playerTable = $(data).find('#ranked_players tbody tr').map(function(i) {
            var row = {};
            $(this).find('td').each(function(i) {
              var rowName = columns[i];
              row[rowName] = $(this).text();
            });
            return row;
          }).get();

          var columns = $(data).find('#prize_info_container table thead th').map(function() {
            return $(this).text().toLowerCase();
          });
          var prizeTable = $(data).find('#prize_info_container table tbody tr').map(function(i) {
            var row = {};
            $(this).find('td').each(function(i) {
              var rowName = columns[i];
              row[rowName] = $.trim($(this).text());
            });
            return row;
          }).get();
          var res = $.extend(true,prizeTable,playerTable);
        console.dir(res);
        l = res.length;
        i = 0;

        function getUsableInfo(){

          playerRank = res[i].rank;
          playerName = res[i].name;
          prizeWon = res[i].prizes.replace(/\$/g, '');
          vURL = "http://www.boostlv.com/b/php/get_playerId.php?pn="+playerName;
          $.ajax({
            url: vURL,
            dataType: 'html',
            success: function(data){
              playerId = data;
              $('#results').append($("<div class='players'>").text("INSERT into `a5225865_rg`.`tourn_results` (`tournamentId`, `playerId`, `playerName', `playerRank`, `prizeWon`) VALUES ('" + tournamentId + "', '" + playerId + "', '" + playerName + "', '" + playerRank + "', '" + prizeWon +"')"));
              i++;
              if(i<l){
                getUsableInfo();
              } else {
                tournamentCurrentId++;
                getTournamentInfo();
              }
            }
          });

        }
        getUsableInfo();


     }



  } else {
    // The current tournament id is the same as the ending, we have finished
    // processing all the tournaments in the range so don't do anything else

  }

    tournamentCurrentId++;
    getTournamentInfo();
}
函数getTournamentInfo(){ /** *我们需要创建一个循环,仅在第一个请求之后触发AJAX *已完成,因此结果不会出现问题和混乱。 *评估当前tournamentid是否大于结束id **/
如果(tournamentCurrentId使用的是无返回语句的递归函数(getTournamentInfo)

那么发生了什么:
getTournamentInfo()在一个从未中断的循环中调用getTournamentInfo(),直到Javascript解释器崩溃并抛出“超出最大调用堆栈大小”错误。

哦,好的,我正在尝试分离函数,以便在getTournamentInfo()之外调用getUsableInfo()(getPlayerInfo())函数。但是我确实需要它来循环。你能推荐最好的方法吗?每次我使用for循环,结果都会出错。