如何避免在与服务器交互的文本框、文本区域中重复请求

发布于 2024-12-03 04:01:19 字数 318 浏览 1 评论 0原文

在 Javascript 中,是否有人/任何人都有要避免的详尽的组合键列表。例如,文本框为每次按键发送 Web 服务请求。这种情况经常发生在诸如 HOME[光标返回文本框开头]、DELETESHIFT + 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清浅ˋ旧时光 2024-12-10 04:01:19

假设用户正在文本区域或输入中书写,您可以检查每次按键的该元素的值;如果它与最后的值不同,则将其发送到服务器。如果相同,则什么也不做。

示例:

function updateOnKeypress(input) {
    var lastValue;
    input.addEventListener('keypress', function() {
        if( input.value.trim() !== lastValue ) {
            // trim() isn't available in every browser, but you can add it
            // Of course, if leading/trailing whitespace is supposed to trigger
            // a request, then skip the trim()
            lastValue = input.value.trim();
            // … send value to server
        }
    });
}

您可能还想使用计时器来限制请求数量,因此并非每个更改都会导致向服务器发出请求。例如,对于每个更改启动一个超时(例如 0.3 秒),然后在计时器执行时发送该值。

function updateOnKeypressDelayed(input) {
    var lastValue, timer;
    input.addEventListener('keypress', function() {
        if( !timer ) {
            timer = setTimeout(function () {
                if( input.value.trim() !== lastValue ) {
                    lastValue = input.value.trim();
                    timer = null;
                    // … send value to server
                }
            }, 300);
        }
    });
}

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:

function updateOnKeypress(input) {
    var lastValue;
    input.addEventListener('keypress', function() {
        if( input.value.trim() !== lastValue ) {
            // trim() isn't available in every browser, but you can add it
            // Of course, if leading/trailing whitespace is supposed to trigger
            // a request, then skip the trim()
            lastValue = input.value.trim();
            // … send value to server
        }
    });
}

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.

function updateOnKeypressDelayed(input) {
    var lastValue, timer;
    input.addEventListener('keypress', function() {
        if( !timer ) {
            timer = setTimeout(function () {
                if( input.value.trim() !== lastValue ) {
                    lastValue = input.value.trim();
                    timer = null;
                    // … send value to server
                }
            }, 300);
        }
    });
}
兰花执着 2024-12-10 04:01:19

与其避免所有这些组合,为什么不限制你接受的内容呢?

例如,创建一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文