繁体   English   中英

如何更新Ajax调用(不包含内容)

[英]How to update Ajax call (not content)

请看下面的代码-在内容被调用后,如何杀死/更新或重新启动ajax调用(不是Ajax调用的内容)?

我的意思是$('#posting_main')被称为onclick并具有动画-如何停止ajax并使其在另一个单击上$('#posting_main')另一个$('#posting_main')

 $(document).ready(function() {

 $("#img_x_ok").click(function(e){
 e.preventDefault();

 var post_text = $.trim($("#main_text_area").val());

 var data_text = 'post_text='+ post_text;
 if (post_text === "") return;

 var xhr = $.ajax({

 type: "POST",
 url: "comm_main_post.php",
 data: data_text,
 cache: false,

 success: function (data){
 //content

 $("#posting_main").fadeIn();
 $("#posting_main").load("pull_comm.php");
 $("#main_text_area").attr("value", "");

 $("#posting_main").animate({ 
     marginTop: "+=130px",
 }, 1000 );

 }

 }); //ajax close

 }); }); //both functions close

您可以使用以下方法中止当前请求

xhr.abort();

完成之后,您可以运行另一个$.ajax(...)发出第二个请求。


您可以像下面这样实现它。 注意缩进代码使其更具可读性!

 $(document).ready(function() {

     var xhr; // by placing it outside the click handler, you don't create
              // a new xhr each time. Rather, you can access the previous xhr
              // and overwrite it this way

     $("#img_x_ok").click(function(e){

         e.preventDefault();

         var post_text = $.trim($("#main_text_area").val());

         var data_text = 'post_text='+ post_text;
         if (post_text === "") return;

         if(xhr) xhr.abort(); // abort current xhr if there is one

         xhr = $.ajax({

             type: "POST",
             url: "comm_main_post.php",
             data: data_text,
             cache: false,

             success: function (data){
                 //content

                 $("#posting_main").fadeIn();
                 $("#posting_main").load("pull_comm.php");
                 $("#main_text_area").attr("value", "");

                 $("#posting_main").animate({ 
                     marginTop: "+=130px",
                 }, 1000 );

             }

         });

     });

});

我不确定我是否完全理解您的问题,但是:

  1. xhr.abort()将终止AJAX请求。 调用abort()之后,可以根据需要修改并重新发送请求。
  2. $(“#posting_main”)。stop()将停止fadeIn动画。 (而且我认为您可能需要在$(“#posting_main”)。hide()之后加上它,以确保它不会部分可见。)

暂无
暂无

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

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