将Ajax请求限制为Foursquare建议完成

时间:2022-10-13 16:06:49

I am using Twitter bootstrap's typeahead via javascript to provide a typeahead for foursquare venues using this snippet of code :

我使用Twitter bootstrap的typeahead通过javascript为foursquare场所使用这段代码提供了一个预先输入:

 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);
                        }
                    );
                }
        });
    }
}

It currently works, but in my Network requests I can see each time I change the query, there is a new XHR request to foursquare. I have tried to minimize them using the (not so elegant) keyup event with the inputQuery length conditional but it is still being called.

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

I would like to know if there is there a way to minimize these requests

我想知道是否有办法最小化这些请求

1 个解决方案

#1


1  

Late to the party, perhaps, but it looks like the new version of Typeahead will help you out here.

或许晚会,但看起来新版本的Typeahead会帮助你。

The new version has, built into its concept of Remote data sources, a rateLimitWait that allows you to cap the number of ms between each call that typeahead will attempt to make.

新版本的远程数据源概念中包含一个rateLimitWait,它允许您限制typeahead尝试进行的每次调用之间的ms数。

It also doesn't look like you need to bind it to keyup, since the examples say that it can listen to changes in the input field all by itself. That will help you avoid double-bindings that could wind up circumventing your rate limiting.

它看起来也不像你需要将它绑定到keyup,因为这些例子说它可以自己监听输入字段中的变化。这将有助于您避免可能最终绕过速率限制的双重绑定。

#1


1  

Late to the party, perhaps, but it looks like the new version of Typeahead will help you out here.

或许晚会,但看起来新版本的Typeahead会帮助你。

The new version has, built into its concept of Remote data sources, a rateLimitWait that allows you to cap the number of ms between each call that typeahead will attempt to make.

新版本的远程数据源概念中包含一个rateLimitWait,它允许您限制typeahead尝试进行的每次调用之间的ms数。

It also doesn't look like you need to bind it to keyup, since the examples say that it can listen to changes in the input field all by itself. That will help you avoid double-bindings that could wind up circumventing your rate limiting.

它看起来也不像你需要将它绑定到keyup,因为这些例子说它可以自己监听输入字段中的变化。这将有助于您避免可能最终绕过速率限制的双重绑定。