1
大家好,我不知道如何解决这个问题。我有一个函数传递了一个HTML img元素的数组。它循环浏览这些图像,使用空白的“无图像”缩略图检查图像的SRC属性。然后使用img标签ALT属性作为查询执行图像搜索。搜索的回调函数然后用第一个图像结果替换Img SRC。如何按执行顺序排列并发的谷歌图片搜索?
我遇到问题匹配正确的图像与相应的搜索回调。现在我只是创建数组并将返回的搜索与图像的索引进行匹配。由于多个搜索同时运行,根据图像大小或网络延迟时间的不同,它们可以按顺序触发回调并混合图像。
我需要一种方法让我将单个搜索与html元素配对。这可能使用searchController和多个imageSearch对象吗?
下面是我使用的函数的例子
google.load('search', '1');
function googleFillBlanks(jqueryImages){
//namePairs holds the images matching alt text and attachedCount is used for matching up once the call back is fired
var attachedCount = 0;
var namePairs = [];
function searchComplete(searcher){
if (searcher.results && searcher.results.length > 0) {
var results = searcher.results;
var result = results[0];
$("img[alt='"+namePairs[attachedCount]+"'] ").attr('src', result.tbUrl);
//jqueryImages.get(0).attr('src', result.tbUrl);
attachedCount++;
}
}
var imageSearch = new google.search.ImageSearch();
//restrict image size
imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE,
google.search.ImageSearch.IMAGESIZE_SMALL);
imageSearch.setSearchCompleteCallback(this, searchComplete, [imageSearch]);
jqueryImages.each(function(){
if($(this).attr('src').substr(-12,8) == 'no_image')
{
namePairs.push($(this).attr('alt'));
imageSearch.execute($(this).attr('alt'));
}
});
}