简体   繁体   English

我怎样才能防止在jQuery中同时发布多个帖子到同一页面?

[英]How can i prevent multiple posts to the same page at the same time in jquery?

i have an input search like this: 我有这样的输入搜索:

<input type="search" autocomplete="off" name="searchSchools" id="searchSchools" value="" onKeyUp="searchSchools(this.value)" />

As you can se, every time the onKeyUp event is fired i call a function named "searchSchools" where i search schools matching the current value of the input search in PHP and i return them in JSON format, the problem is that if i type fast more than one request is sent to the PHP page and i want to prevent this, i want to send the post request to the page only if there is no other request to the same page made by this function. 如您所知,每次触发onKeyUp事件时,我都会调用一个名为“ searchSchools”的函数,在该函数中,我搜索与PHP中输入搜索的当前值匹配的学校,并以JSON格式返回它们,问题是如果我快速键入一个以上的请求被发送到PHP页面,并且我想防止这种情况,只有在此功能对同一页面没有其他请求的情况下,我才希望将发布请求发送到该页面。

Here is the function: 这是函数:

function searchSchools(name){
    $.post("search-schools.php", { name: name },
    function(data) {
        //-> Here i do whatever with data returned
    }, 
    "json"
    );
}

Add a delay in $.post .. Try like below, $.post添加延迟。

var timer = null, xhrReq = null;
function searchSchools(name){
    if (timer != null) {
       clearTimeout(timer);
    }

    timer = setTimeout(function () {
        if(xhrReq != null) {
           xhrReq.abort(); //to kill all prev request and take the latest
        }

        xhrReq = $.post("search-schools.php", { name: name },
           function(data) {
              //-> Here i do whatever with data returned
           }, 
           "json"
         );
    }, 1000); //will be posted only after 1 second.
}

Edit: Added xhr.abort() to abort any old request and consider only the latest.. 编辑:添加了xhr.abort()来中止任何旧请求并仅考虑最新请求。

var request;
function searchSchools(name){
    if (!request) {
       request = $.post("search-schools.php", { name: name },
       function(data) {
           request = false;
           //-> Here i do whatever with data returned
       }, 
       "json"
       );
    }
}

Assuming you don't want the behavior I pointed out in my first comment on the OP 假设您不希望我在对OP的第一条评论中指出的行为

I like to approach this kind of problem in a more wasteful manner: 我喜欢以更浪费的方式解决此类问题:

var xhr;
function search(term) {
    if (xhr) {
        xhr.abort();
    }

    xhr = $.post(...);
}

This makes the basic logic much easier to handle since the success callbacks of aborted requests won't get called and you always get the most current result regarding the actual input. 这将使基本逻辑更易于处理,因为不会调用中止请求的成功回调,并且您始终可以获得有关实际输入的最新结果。

If you are wary of starting and aborting potentially many request, introduce a throttler on the appropriate key event - thus also achieving a seperation of the concerns. 如果您对启动和中止可能的许多请求感到担心,请在适当的按键事件上引入调节器-这样也可以将关注点分离。

Give xhr the proper context in your script - it is global in this example. 在脚本中为xhr适当的上下文-在此示例中为全局。 You want to avoid that. 您要避免这种情况。

EDIT1 - added: EDIT1-添加:

xhr is never unset. xhr从未设置过。 You might want to do this in in the complete handler of the jQuery AJAX settings object. 您可能希望在jQuery AJAX设置对象的complete处理程序中执行此操作。 On the other hand, calling abort() on an aborted or completed request is - in effect - a NOP 另一方面,在已终止或已完成的请求上调用abort()实际上是一个NOP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM