[英]Autocomplete getting data from API
I want to create an autocomplete input component that retrieves places from Google Maps Place API. 我想创建一个自动完成输入组件,以从Google Maps Place API检索位置。
This is a sample response from this API: https://developers.google.com/places/web-service/search#PlaceSearchResponses 这是来自以下API的示例响应: https : //developers.google.com/places/web-service/search#PlaceSearchResponses
My question is how should I handle user input / typing and calling the API to update the autocomplete options? 我的问题是我应该如何处理用户输入/键入和调用API以更新自动完成选项?
Would it be correct to do something like: On keyup
set a timeout, to check if the user stopped typing, then call the API after a few seconds; 这样做是正确的吗:在
keyup
设置超时,以检查用户是否停止键入,然后在几秒钟后调用API; and on keydown
clear the timeout to avoid calling the API while the user is still typing? 并在
keydown
清除超时以避免在用户仍在键入时调用API?
I don't have access to Google Cloud Platform Console, so I can't enable Maps Javascript API and use Place Autocomplete 我无权访问Google Cloud Platform Console,因此无法启用Maps Javascript API并使用“ 地方自动完成”功能
Are you using pure javascript? 您是否使用纯JavaScript? If not, yout could use lodash debounce function , it does exactly what you need.
如果没有,您可以使用lodash 防反跳功能 ,它完全可以满足您的需求。
EDIT: As someone pointed out in the comments, i shouldn't just post a link as an answer, so i'll provide a snippet to help improve this answer 编辑:正如某人在评论中指出的那样,我不应该只是发布链接作为答案,所以我将提供一个片段以帮助改善此答案
import _ from 'lodash';
$(selector).on('keyup', _.debounce(function (e) {
// stuff
}
}, 500));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.