【发布时间】:2012-12-14 14:47:26
【问题描述】:
我正在通过 javascript 使用 Twitter bootstrap 的 typeahead 来为使用这个 sn-p 代码的foursquare场地提供一个 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 事件来最小化它们,但它仍在被调用。
我想知道是否有办法最小化这些请求
【问题讨论】:
标签: javascript ajax jquery twitter-bootstrap foursquare