我创建了一个简单的 chrome 扩展,它迭代用户浏览历史并计算与特定编程语言数组匹配的结果数量。问题是我想迭代数组的长度并为数组列表中的每个字符串调用一个异步 chrome 方法。扩展可以在这里找到: 你是什么类型的程序员? 有问题的代码是:
function myfunction() {
var listOfLanguages = ["Java", "C", "C++", "PHP", "C#", "Visual Basic", "Python", "Objective-C", "Perl", "Javascript", "Ruby"];
var mostPopular = "not a programmer";
var totalResults = 0;
for (var i = 0, len = listOfLanguages.length; i < len; i++) {
(function (i) {
setTimeout(function () {
chrome.history.search({
text: listOfLanguages[i],
maxResults: 500
}, function (callback) {
var countOfResults = callback.length;
var langOfResults = listOfLanguages[i];
if (countOfResults > totalResults) {
totalResults = countOfResults;
mostPopular = langOfResults;
}
if (i == 10) {
var lang = mostPopular;
console.log(mostPopular);
switch (lang) {
case "Java":
document.write('<img src="assets/images/java.png"/>');
break;
case "C#":
document.write('<img src="assets/images/C#.jpg"/>');
break;
case "C":
document.write('<img src="assets/images/C.png"/>');
break;
case "C++":
document.write('<img src="assets/images/c++.png"/>');
break;
case "Objective-C":
document.write('<img src="assets/images/objectivec.png"/>');
break;
case "Perl":
document.write('<img src="assets/images/perl.gif"/>');
break;
case "PHP":
document.write('<img src="assets/images/php.png"/>');
break;
case "Python":
document.write('<img src="assets/images/python.jpg"/>');
break;
case "Ruby":
document.write('<img src="assets/images/ruby.png"/>');
break;
case "Visual Basic":
document.write('<img src="assets/images/vb.png"/>');
default:
document.write('<img src="assets/images/noprog.png"/>');
}
}
});
}, i * 1000);
})(i);
}
}
window.onload = myfunction;
我最终通过在每次迭代中设置超时函数找到了解决方法,但这不是一个优雅的解决方案。我已经在网上阅读了很多文章来处理这个问题,它们涉及添加更多我不理解的迭代级别。希望有人可以解释处理这个问题的正确方法,很多人都遇到了。