【问题标题】:Limit Ajax Requests to Foursquare suggestcompletion将 Ajax 请求限制为 Foursquare 建议完成
【发布时间】: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


    【解决方案1】:

    也许迟到了,但看起来the new version of Typeahead 会在这里帮助你。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多