如何避免在与服务器交互的文本框、文本区域中重复请求
在 Javascript 中,是否有人/任何人都有要避免的详尽的组合键列表。例如,文本框为每次按键发送 Web 服务请求。这种情况经常发生在诸如 HOME
[光标返回文本框开头]、DELETE
、SHIFT + HOME + DELETE
等组合键上,事件会触发并且相同的请求参数被发送到 webservice。[我知道缓存查询结果将解决这种情况,但其他情况如何]。我很想看到一个列表,之前我找不到类似的问题,因此这个 更新: 由于之前的问题标题似乎太离谱并且可能可以关闭,因此我更改了它。我想知道如何优化以ajax方式发送请求的过程,以降低强度。
In Javascript, does someone/anyone have a exhaustive list of key combination to avoid. For example a textbox sending webservice requests for each keypress. It often happens for key combinations like HOME
[for cursor returning to start of textbox] , DELETE
, SHIFT + HOME + DELETE
the event fires and same request params is sent to webservice.[I know caching query results will solve for this scenario but what about other]. I would love to see a list for this, i couldn't find a similar question before hence this
Update:
Since the earlier question title seemed too way off and likely closable i changed it. I would like to know how to optimize this process of sending request in ajax manner less intensive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设用户正在文本区域或输入中书写,您可以检查每次按键的该元素的值;如果它与最后的值不同,则将其发送到服务器。如果相同,则什么也不做。
示例:
您可能还想使用计时器来限制请求数量,因此并非每个更改都会导致向服务器发出请求。例如,对于每个更改启动一个超时(例如 0.3 秒),然后在计时器执行时发送该值。
Assuming the user is writing in a textarea or input, you can check the value of that element for each keystroke; if it's different from its last value, send it to the server. If it's the same, then do nothing.
Example:
You might also want to throttle the number of requests with a timer, so not every change results in a request to the server. For instance, for each change start a timeout (say, 0.3 seconds), and then send the value when the timer executes.
与其避免所有这些组合,为什么不限制你接受的内容呢?
例如,创建一个 JS 函数来检查这是否是字符、字母或特殊字符,如果不是,则不提交到服务器。
Instead of avoiding all those combinations, why don't you just limit what you accept.
For example make a JS function that checks if this is a character, letter or special character, if not then do not submit to server.