gpt4 book ai didi

javascript - 如何重复查询?

转载 作者:行者123 更新时间:2023-12-03 05:24:27 24 4
gpt4 key购买 nike

我需要重复以下查询以增加谷歌图像的结果。单击按钮后将执行代码。

Google 文档说:

Google Custom Search and Google Site Search return up to 10 results per query. If you want to display more than 10 results to the user, you can issue multiple requests (using the start=0, start=11 ... parameters) and display the results on a single page. In this case, Google will consider each request as a separate query, and if you are using Google Site Search, each query will count towards your limit.

Other SO question says

To get more result you should make multiple calls. in each different call, increase the value of parameter 'start' by 10.

还有this one says :

Now you can not ask google for more than 10 results at a time. So you have to query again asking for 10 result starting from 11. So In next query, keep num=10 and start=11. Now you can get all the results by changing start value.

如何重复以下操作并每次增加 start 参数?

    function googleImages() {
var myCx = "MY_CX";
var myKey = "MY_KEY";
$.getJSON("https://www.googleapis.com/customsearch/v1", {
q: termS + " in 1930 in " + country,
alt: "json",
searchType: "image",
cx: myCx,
num: 10,
start: 0,
key: myKey,
rights: "cc_publicdomain",
filter: "1",
safe: "high",
imgType: "photo",
fileType: "jpg"
},
function (data) {
$.each(data.items, function(i,item) {
$(".my_images .row .grid").append('<div class="col-sm-4 grid-item"><div class="thumbnail"><img class="img-responsive" src="' + item.link + '"></div></div>');
});
var $grid = $('.grid').packery({
// options
itemSelector: '.grid-item',
percentPosition: true
});
$grid.imagesLoaded().progress( function() {
$grid.packery('layout');
});
});
}

最佳答案

您应该在调用之间保存start值,例如您可以使用如下模式:

   function createGoogleImagesLoader(initialValue) {
var _start = initialValue || 0;
var imagesCount = 10;

return function() {
$.getJSON("https://www.googleapis.com/customsearch/v1", {
...
num: imagesCount,
start: _start
},..);
_start += imagesCount;

}
}

var googleImages = createGoogleImagesLoader();
googleImages() // load from 0 to 10
googleImages() // load from 10 to 20 etc.

关于javascript - 如何重复查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41211341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com