【问题标题】:$.get and callback executed before its time$.get 和回调在其时间之前执行
【发布时间】:2011-04-11 12:49:21
【问题描述】:

这个函数,使用 $.get 加载内容,在回调中我调用一个 jquery 插件(翻转)函数,用新内容“播放”(产生动画效果),

function editar_item(id, tipo) {
    var div = 'link';
    if (tipo == 0) {} else if (tipo == 1) {
        div = 'pregunta';
    } else if (tipo == 2) {
        div = 'file';
    }

    $.get('/includes/router.php?que=editar_item&id=' + id + '&tipo=' + tipo, function (data) {
        $("#" + div + id).flip({
            direction: 'tb',
            content: data,
            color: '#F9F9F9',
            onEnd: function () {
                console.log('when the animation has already ended');
            }
        });

    });
    return false;
}

它可以工作,但是 jquery 插件在加载内容之前调用它,我无法正确看到“效果”,

知道为什么吗?

【问题讨论】:

    标签: jquery callback get


    【解决方案1】:

    你的回调应该是第三个参数,你使用它作为第二个。试试:

    $.get('/includes/router.php?que=editar_item&id='+id+'&tipo='+tipo, {},  function(data) {
       // ...etc
    

    【讨论】:

    • AFAIK 数据参数是可选的。但是,您应该使用它而不是手动构建查询字符串。
    • 同意,这是可选的,但您需要在该插槽中指定一些内容,以便不会将回调分配给错误的参数。
    • 我认为 jQuery 足够聪明,可以测试第二个参数是否可调用,在这种情况下将其用作成功回调而不是数据。
    • 我得到了相同的结果(缓存被清除),是否在正确的地方有'return false'?
    猜你喜欢
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    相关资源
    最近更新 更多