我使用rottentomatoes电影API与twitter的typeahead插件一起使用bootstrap 2.0。我已经能够整合这个API,但是我遇到的问题是,在API被调用之后的每个keyup事件之后。这一切都很好,但是我宁愿在一小段暂停之后打电话,让用户先输入几个字符。我可以推迟jquery的关键事件吗?
这是我的一个keyup事件后调用API当前代码:
var autocomplete = $('#searchinput').typeahead()
.on('keyup', function(ev){
ev.stopPropagation();
ev.preventDefault();
//filter out up/down, tab, enter, and escape keys
if($.inArray(ev.keyCode,[40,38,9,13,27]) === -1){
var self = $(this);
//set typeahead source to empty
self.data('typeahead').source = [];
//active used so we aren't triggering duplicate keyup events
if(!self.data('active') && self.val().length > 0){
self.data('active', true);
//Do data request. Insert your own API logic here.
$.getJSON("http://api.rottentomatoes.com/api/public/v1.0/movies.json?callback=?&apikey=MY_API_KEY&page_limit=5",{
q: encodeURI($(this).val())
}, function(data) {
//set this to true when your callback executes
self.data('active',true);
//Filter out your own parameters. Populate them into an array, since this is what typeahead's source requires
var arr = [],
i=0;
var movies = data.movies;
$.each(movies, function(index, movie) {
arr[i] = movie.title
i++;
});
//set your results into the typehead's source
self.data('typeahead').source = arr;
//trigger keyup on the typeahead to make it search
self.trigger('keyup');
//All done, set to false to prepare for the next remote query.
self.data('active', false);
});
}
}
});
是否可以设置一个小的延迟,避免每次KEYUP后调用API?
这工作的很好,但它似乎陷入了一个循环,每200毫秒打一次数据请求调用? – Paul 2012-04-01 18:23:14
在你的代码中,你在keyup后触发了keyup:'self.trigger('keyup')'。对于正确的键盘事件处理,你应该删除'ev.stopPropagation();'和'self.trigger('keyup')' – 2012-04-01 19:20:12
这么做了......谢谢! – Paul 2012-04-02 12:07:47