Live search optimisation in Javascript

蹲街弑〆低调 提交于 2019-12-25 01:43:24

问题


I have some code in Javascript (not jQuery, unfortunately) at the moment that performs a live search on a database, depending on what the user enters.

The problem is, if you type quickly, it'll still be performing the search from the last keystroke and this can add up to a delay of anything up to ten seconds.

I know I should cache this information, and it's definitely something I'd love to do soon (along with implementing jQuery) but for now I was wondering if there was any way at all to speed this up?

I've already limited the number of rows the search returns to 20, and have made sure the search doesn't run unless there are 3 or more characters.

Thanks


回答1:


Make the ajax call trigger 1 or 2 seconds after the user stopped typing.

If you need code for this let me know in the comments.




回答2:


You can check if there's already a request running. If so, abort it. (currentRequest.abort())



来源:https://stackoverflow.com/questions/7528050/live-search-optimisation-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!