我的部分功能未执行,我知道代码很可怕,我试图使其更高效。
Chrome开发者控制台错误:
未捕获的RangeError:超出最大调用堆栈大小
我已尝试评论要完成的工作。我希望这有帮助。
我正在从网站上抓取数据(大量数据,大约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)。
那么发生了什么:
getTournamentInfo()在永不中断的循环中调用getTournamentInfo(),直到Javascript解释器崩溃并引发“超出最大调用堆栈大小”错误为止。