简体   繁体   English

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

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

Look at this code please - how could I kill / update or restart an ajax call (not content that Ajax calls) after the content has already been called? 请看下面的代码-在内容被调用后,如何杀死/更新或重新启动ajax调用(不是Ajax调用的内容)?

I mean the $('#posting_main') is called onclick and animated - how to stop ajax and make it another $('#posting_main') on another click? 我的意思是$('#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

You can abort the current request with: 您可以使用以下方法中止当前请求

xhr.abort();

After having done that, you can run another $.ajax(...) to make a second request. 完成之后,您可以运行另一个$.ajax(...)发出第二个请求。


You could implement it like the following. 您可以像下面这样实现它。 Note that indenting code makes it a lot more readable! 注意缩进代码使其更具可读性!

 $(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 );

             }

         });

     });

});

I am not sure I fully understand your question, however: 我不确定我是否完全理解您的问题,但是:

  1. xhr.abort() will kill the AJAX request. xhr.abort()将终止AJAX请求。 After calling abort(), you could modify and resend the request, if desired. 调用abort()之后,可以根据需要修改并重新发送请求。
  2. $("#posting_main").stop() will stop the fadeIn animation. $(“#posting_main”)。stop()将停止fadeIn动画。 (And I think you might need to follow that with $("#posting_main").hide() to be sure it isn't left partially visible.) (而且我认为您可能需要在$(“#posting_main”)。hide()之后加上它,以确保它不会部分可见。)

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

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