gpt4 book ai didi

javascript - 将 Ajax 请求限制为 Foursquare suggestcompletion

转载 作者:行者123 更新时间:2023-11-30 05:52:28 25 4
gpt4 key购买 nike

我正在通过 javascript 使用 Twitter bootstrap 的 typeahead,使用这段代码为 foursquare field 提供 typeahead:

 function addLocationTypeaheadHandler() {
$('input#location').keyup(function() {callFoursquareForTypeahead()});
}

function callFoursquareForTypeahead() {
var inputQuery = $('input#location').val();
if (inputQuery.length == 3) {
$('input#location').typeahead({
source: function(query, process) {
var urlString = "https://api.foursquare.com/v2/venues/suggestcompletion?ll=" + $('#latitude-field').val() + "," + $('#longitude-field').val() +
"&radius=1000&client_id=" + clientid + "&client_secret=" + clientsec;
return $.get(urlString, {query: $('input#location').val()},
function(json) {
venueNames = [];
$.each(json.response.minivenues, function(index,value) {
venueNames.push(value.name);
});
return process(venueNames);
}
);
}
});
}
}

它目前有效,但在我的网络请求中我可以看到每次更改查询时,都会有一个新的 XHR 请求到 foursquare。我尝试使用带有 inputQuery 长度条件的(不太优雅的)keyup 事件来最小化它们,但它仍在被调用。

我想知道是否有办法尽量减少这些请求

最佳答案

派对可能迟到了,但它看起来像 the new version of Typeahead会在这里帮助你。

新版本在其概念中内置了 Remote data sources ,一个 rateLimitWait,它允许您限制 typeahead 将尝试进行的每次调用之间的毫秒数。

它看起来也不需要将它绑定(bind)到 keyup,因为 the examples say它可以自己监听输入字段的变化。这将帮助您避免可能最终绕过速率限制的双重绑定(bind)。

关于javascript - 将 Ajax 请求限制为 Foursquare suggestcompletion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13880897/

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