weixin_33696822 2011-09-11 10:21 采纳率: 0%
浏览 25

ajax重复先前的请求

I'm working with codeigntier and i'm trying some stuff with ajax. This is somewhat difficult to explain.

I have a controller Products with a method "overview", and a view "products_overview".

This is the controller

This is the view

My problem is, is when I make the ajax call in the view in this part:

$('body').delegate('#notification-close', 'click', function(){
    $('#notification').fadeOut(200, function(){
        $('#notification').remove();
    });

    $('#blanket').fadeOut(200).remove();

    $.ajax({
        type: 'GET',
        url: '<?php echo $current_get_url; ?>',
        success : function (result) {
            $('#column-middle').html(result);
        }
    });

});

It doubles the ajax call everytime i use it. And something else. The products controller creates pagination links. When I go back and forth a few times, I also make ajax calls. Let's say I do that 4 times. Then, when I use the above ajax call, it will execute those 4 previous calls and then start doubling from that!

So, i'm kind of lost here. When I put a setTimeout on $('#column-middle').html(result), it will execute once, but then give a jQuery error that "result" is not defined.

  • 写回答

2条回答

  • weixin_33749131 2011-09-11 10:26
    关注

    You should return false from a delegate handler function to stop event bubbling, e.g.:

    $('body').delegate('#notification-close', 'click', function(){
        // processing
    
        // stop further handlers from executing
        return false;
    });
    

    See caveats section here. You can also abuse closure scope properties to prevent call doubling:

    var column_middle_working = false;
    $('body').delegate('#notification-close', 'click', function(){
        if (column_middle_working) return;
        column_middle_working = true;
        // do what you do
    
        $.ajax({
            type: 'GET',
            url: '<?php echo $current_get_url; ?>',
            success : function (result) {
                $('#column-middle').html(result);
                column_middle_working = false;
            }
        });
    
    });
    

    But still recommend you finding a real reason of this behavior.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog