Javascript 将Ajax请求限制为Foursquare suggestcompletion
我正在通过javascript使用Twitter bootstrap的typeahead为foursquare场地提供一个typeahead,使用以下代码片段:Javascript 将Ajax请求限制为Foursquare suggestcompletion,javascript,ajax,jquery,twitter-bootstrap,foursquare,Javascript,Ajax,Jquery,Twitter Bootstrap,Foursquare,我正在通过javascript使用Twitter bootstrap的typeahead为foursquare场地提供一个typeahead,使用以下代码片段: function addLocationTypeaheadHandler() { $('input#location').keyup(function() {callFoursquareForTypeahead()}); } function callFoursquareForTypeahead() { var in
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);
}
);
}
});
}
}
它目前可以工作,但在我的网络请求中,我每次更改查询时都可以看到一个新的对foursquare的XHR请求。我已经尝试使用带有inputQuery长度条件的(不太优雅的)keyup事件来最小化它们,但它仍然被调用
我想知道是否有办法尽量减少这些晚到派对的请求,也许,但它看起来会帮到你 新版本在其rateLimitWait的概念中内置了rateLimitWait,允许您限制typeahead尝试进行的每次呼叫之间的毫秒数 它看起来也不需要将它绑定到keyup,因为它可以自己监听输入字段中的更改。这将帮助您避免双重绑定,最终可能绕过您的速率限制