我将如何使这段代码高效?
How would I make this code efficient?
首先,很抱歉它没有那么整洁,我仍在学习 javascript 的绳索。
我在 google chrome 控制台中 运行ning 这段代码,但是 运行 它需要太多时间,我在做什么,我可以修复它以使其 运行 更快?
function snipebot(page, max_page, max_price){
$.getJSON('http://www.roblox.com/catalog/json?browse.aspx?Subcategory=2&Keyword=&CurrencyType=0&pxMin=0&pxMax=0&SortType=2&SortAggregation=0&SortCurrency=0&LegendExpanded=true&Category=2&PageNumber=' + page, function(data){
$.each(data, function(index, item){
if (item['BestPrice'] <= max_price){
$.get('http://www.roblox.com/Item.aspx?id=' + item['AssetId'], function(data){
var purchaseData = $($(data).find(".PurchaseButton")[0]).data();
if (purchaseData['expectedPrice'] <= item['BestPrice']){
$.post('/API/Item.ashx?rqtype=purchase&productID=' + purchaseData['productId'] + '&expectedCurrency=1&expectedPrice=' + purchaseData['expectedPrice'] + '&expectedSellerId=' + purchaseData['expectedSellerId'] + '&userAssetID=' + purchaseData['userassetId'], function(){
console.log('[' + item['BestPrice'] + ']' + item['Name'] + item['AssetId'] + ' user:' + purchaseData['seller-name'] + ' ' + '@' + new Date().toTimeString())
});
} else {
console.log("Detected purchase:" + item['Name'] + item['AssetId'] + ' ' + purchaseData['seller-name']);
}
});
};
});
setTimeout(function(){
snipebot(page + 1 > max_page ? 1 : page + 1, max_page, max_price);
});
});
};
snipebot(1, 4, 50);
在最坏的情况下,对 snipebot()
的调用会往返后端 3 次。您应该检查您的设计并将您的一些逻辑移至后端以减少往返。
if (item['BestPrice'] <= max_price){ ...
if (purchaseData['expectedPrice'] <= item['BestPrice']){ ...
以上两种情况都可以在后端进行测试和处理。
首先,很抱歉它没有那么整洁,我仍在学习 javascript 的绳索。
我在 google chrome 控制台中 运行ning 这段代码,但是 运行 它需要太多时间,我在做什么,我可以修复它以使其 运行 更快?
function snipebot(page, max_page, max_price){
$.getJSON('http://www.roblox.com/catalog/json?browse.aspx?Subcategory=2&Keyword=&CurrencyType=0&pxMin=0&pxMax=0&SortType=2&SortAggregation=0&SortCurrency=0&LegendExpanded=true&Category=2&PageNumber=' + page, function(data){
$.each(data, function(index, item){
if (item['BestPrice'] <= max_price){
$.get('http://www.roblox.com/Item.aspx?id=' + item['AssetId'], function(data){
var purchaseData = $($(data).find(".PurchaseButton")[0]).data();
if (purchaseData['expectedPrice'] <= item['BestPrice']){
$.post('/API/Item.ashx?rqtype=purchase&productID=' + purchaseData['productId'] + '&expectedCurrency=1&expectedPrice=' + purchaseData['expectedPrice'] + '&expectedSellerId=' + purchaseData['expectedSellerId'] + '&userAssetID=' + purchaseData['userassetId'], function(){
console.log('[' + item['BestPrice'] + ']' + item['Name'] + item['AssetId'] + ' user:' + purchaseData['seller-name'] + ' ' + '@' + new Date().toTimeString())
});
} else {
console.log("Detected purchase:" + item['Name'] + item['AssetId'] + ' ' + purchaseData['seller-name']);
}
});
};
});
setTimeout(function(){
snipebot(page + 1 > max_page ? 1 : page + 1, max_page, max_price);
});
});
};
snipebot(1, 4, 50);
在最坏的情况下,对 snipebot()
的调用会往返后端 3 次。您应该检查您的设计并将您的一些逻辑移至后端以减少往返。
if (item['BestPrice'] <= max_price){ ...
if (purchaseData['expectedPrice'] <= item['BestPrice']){ ...
以上两种情况都可以在后端进行测试和处理。