javascript - 超出最大堆栈大小 - 多个 AJAX 循环问题,并且函数未触发

标签 javascript jquery ajax

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

Chrome 开发者控制台错误:

Uncaught RangeError: Maximum call stack size exceeded

我尝试对我想要完成的任务发表评论。我希望这会有所帮助。

我正在从网站上抓取数据(大量数据,大约 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();
}

最佳答案

您正在使用没有 return 语句的递归函数 (getTournamentInfo)。

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

关于javascript - 超出最大堆栈大小 - 多个 AJAX 循环问题,并且函数未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30136126/

相关文章:

javascript - JQuery 在开始动画之前设置旋转位置

javascript - jQuery .data() 被覆盖

javascript - Galaxy s3 打破了响应式布局

javascript - 允许对 php 的进一步 ajax 请求,同时继续处理第一个请求

javascript - 从对象数组中过滤

javascript - 使用默认代码在 angularjs 中设置InfiniteScroll

jquery - 绝对位置的高度不起作用并停止 jquery 工作(示例)

javascript - 从输入字段中获取值并将其传递给 Paypal

由于 XSS 保护,Javascript 未被执行

javascript - 如何在 Zend Framework 2 中通过 AJAX 表单上传文件?