怎样防止重复发送 Ajax 请求?

 

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="GBK">
    </head>
    <body>
        <input type="button" id="btnTest" value="测试"/>
        <script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
        <script>
            $('#btnTest').click(function() {
                if (this.ajaxRequest_ != undefined && this.ajaxRequest_.readyState < 4) {
                    alert('wait');
                    return false;
                }

                this.ajaxRequest_ = $.ajax({
                    type: "POST",
                    url: "homepage/test",
                    success: function(msg) {
                        alert(msg);
                    }
                })
            });
        </script>
    </body>
</html>

 

 

#php接口返回的消息
public
function test() { sleep(5); echo 'success'; }

大神的解决方案:http://ambar.li/blog/2013/04/17/avoid-duplicate-ajax-calls/

我使用了A独占型

posted @ 2014-03-25 14:23  耐得住寂寞  阅读(3008)  评论(0编辑  收藏  举报