feat(*): add debounce #49
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
研究了一下前端搜索的 bug,主要有下:
fwt
,会有两次请求,一次是fw
,返回访问
有关的词条,第二次是fwt
,就是正常的fwt
。因为要请求搜索服务器,所以会慢一点,而且顺序不分先后,很大概率会直接返回fw——访问
的词条。所以这个 pr 修改了两个地方。一个是添加了输入框防抖;另一个是禁用了
enter
默认是选择最优项(可以用上下键选择之后再 enter),这是因为有人搜索习惯性输入一个词之后直接 enter,那这样防抖也没办法处理,肯定会访问错误的页面,服务器返回的速度肯定赶不上 enter 的速度,所以就把这个快捷键 ban 掉了。当然,实现非常丑陋。禁用
enter
这个是直接将对应代码注释了;而输入框防抖改起来太难了,我研究了很久都改不掉 😫,现在的处理方式是添加一个最先执行的脚本 hook 住 addEventListener,再往搜索框添加keyup
事件时会下一个钩子:要防抖之后才能执行。如果有更好的改法,欢迎交流 ❤️。