javascript - 将 Ajax 请求限制为 Foursquare suggestcompletion

标签 javascript ajax jquery twitter-bootstrap foursquare

我正在通过 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/

相关文章:

javascript - 服务器收到请求时的 AJAX 事件 - 在处理请求之前

javascript - 如何使用ajax从一个php页面获取数据并使用ajax将其传递到另一个php页面

使用对象和自执行匿名函数的 jQuery 命名空间

javascript - 如何使用 JQuery 选择此元素?

php - 当用户在 Facebook 评论框上添加评论时发送电子邮件

javascript - php-ajax : how to use two submit button on form every one work for different purpose

python - Gmail 是如何做到的?

jquery - 在 Internet Explorer 中使用 setInterval - 不起作用? jQuery

javascript - 在javascript中将变量分配给自身有什么隐藏的好处吗? "force reflow"?

javascript - 使用 Web 插件时,UFT 如何执行 Click 方法?