高阳的技术专栏 gy

防止按钮多次发送请求

2018-07-18
gaoyang

阅读:

js

防止按钮多次发送请求

   $(function () {
      let flag = false;
      let xmlHttp = null;
      $('#btn').tap(function () {
        if(flag){
          return;
        }
        flag = true;
        $(this).css('background', 'gray');
        // $(this).prop('disabled', true);
        console.log('xxx');
        setTimeout(function () {
          $('#btn').css('background', 'red');
          flag = false;
        }, 2000);

        if(!xmlHttp){ // 第一次发送
          xmlHttp = sendXml('http://localhost:3000', 'GET');
        }else {  // 不是第一次发送
          xmlHttp.abort();
          xmlHttp = sendXml('http://localhost:3000', 'GET');
        }
      });
      function sendXml(url, methods) {
        let xmlHttp = null;
        xmlHttp  = $.ajax({
          url: url,
          type: methods,
          success: function (data) {
            console.log(data);
          }
        });
        return xmlHttp;
      }
    });

上一篇 react笔记

下一篇 vue常用组件

Comments

Content