简体   繁体   English

成功响应来自 Ajax 调用的数据后,还要处理额外的 function

[英]Upon success response with data from Ajax call, process additional function as well

I am trying to call function upon successful response from ajax call.在 ajax 呼叫成功响应后,我试图呼叫 function。

Here both functions:这里有两个功能:

      function waggle() {
       var element = $(this);
          var tmpClass = element.attr('class');
          element.removeClass();
          setTimeout(function() {
            element.offsetWidth = element.offsetWidth;
            element.addClass(tmpClass).addClass('start-now');
          }, 10);
    }

    function auto_load(){
    $.ajax({
      type: 'POST',
      data: {room_id:'$r_room_id',site_id:'$r_site_id'},
      url: 'cart_counter.php',
      success: function(data){
        $('#cart_buble').html(data);
        waggle();
      }
    });
  }

This does update #cart_buble div as expected with the number in the cart however doesn't execute waggle function which is responsible for calling css transition effect.这确实使用购物车中的数字按预期更新了#cart_buble div,但是不会执行waggle function ,它负责调用 css 过渡效果。

Both functions are working fine seperately.这两个功能分别运行良好。

I think this variable is the problem here.我认为this变量是这里的问题。 This kind of function declaration can change the data stored in this variable up to where the function is executed.这种 function 声明可以将存储在this变量中的数据更改为执行 function 的位置。

Try this way试试这个方法

function waggle() {
       var element = $(...); // <--- Query your element instead of using `this`
          var tmpClass = element.attr('class');
          element.removeClass();
          setTimeout(function() {
            element.offsetWidth = element.offsetWidth;
            element.addClass(tmpClass).addClass('start-now');
          }, 10);
    }

    function auto_load(){
    $.ajax({
      type: 'POST',
      data: {room_id:'$r_room_id',site_id:'$r_site_id'},
      url: 'cart_counter.php',
      success: function(data){
        $('#cart_buble').html(data);
        waggle();
      }
    });
  }

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

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