如何编写接受回调的jquery函数

如何编写接受回调的jquery函数,jquery,Jquery,我编写了一个自定义jquery函数,它接受回调,但无法理解当动画函数执行结束时回调函数将如何在调用环境中执行 这是我的自定义函数代码 jQuery.fn.busyToggle = function (flag, marginBottom, opacity, speed, easing, callback) { if (flag == 1) { return this.stop(true).animate({ marginBottom: marginBott

我编写了一个自定义jquery函数,它接受回调,但无法理解当动画函数执行结束时回调函数将如何在调用环境中执行

这是我的自定义函数代码

jQuery.fn.busyToggle = function (flag, marginBottom, opacity, speed, easing, callback) {
        if (flag == 1) {
            return this.stop(true).animate({ marginBottom: marginBottom, opacity: opacity }, { queue: false, duration: speed });
        }
        else {
            return this.stop(true).animate({ marginBottom: marginBottom, opacity: opacity }, { queue: false, duration: speed });
        }
    };
这样我调用这个函数

$('#BusyBox').busyToggle(flag,0,1,500);

我需要知道如何从调用环境捕获动画函数结束的时间。如果可能,请详细讨论。谢谢

一种方法是使用
.animate()提供的回调函数
另一种方法是将其添加到链队列中,如下所示:

jQuery.fn.busyToggle = function(flag, marginBottom, opacity, speed, easing, callback) {
    if (flag == 1) {
        return this.stop(true).animate({
            marginBottom: marginBottom,
            opacity: opacity
        }, {
            queue: false,
            duration: speed
        }).queue(callback);
    }
    else {
        return this.stop(true).animate({
            marginBottom: marginBottom,
            opacity: opacity
        }, {
            queue: false,
            duration: speed
        }).queue(callback);
    }
};​

一种方法是使用
.animate()
另一种方法是将其添加到链队列中,如下所示:

jQuery.fn.busyToggle = function(flag, marginBottom, opacity, speed, easing, callback) {
    if (flag == 1) {
        return this.stop(true).animate({
            marginBottom: marginBottom,
            opacity: opacity
        }, {
            queue: false,
            duration: speed
        }).queue(callback);
    }
    else {
        return this.stop(true).animate({
            marginBottom: marginBottom,
            opacity: opacity
        }, {
            queue: false,
            duration: speed
        }).queue(callback);
    }
};​

只需将回调添加到
animate

jQuery.fn.busyToggle = function (flag, marginBottom, opacity, speed, easing, callback) {
    ...
    this.stop(true).animate({ marginBottom: marginBottom, opacity: opacity }, { queue: false, duration: speed, complete: callback });
};

$('#BusyBox').busyToggle(flag,0,1,500, function(){ 
    // do something on complete
});

只需将回调添加到
animate

jQuery.fn.busyToggle = function (flag, marginBottom, opacity, speed, easing, callback) {
    ...
    this.stop(true).animate({ marginBottom: marginBottom, opacity: opacity }, { queue: false, duration: speed, complete: callback });
};

$('#BusyBox').busyToggle(flag,0,1,500, function(){ 
    // do something on complete
});

接受回调作为参数。只需将回调传递给它即可。看看这些例子。接受回调作为参数。只需将回调传递给它即可。看看这些例子。当我调用busyToggle()时,你没有说如何附加回调函数……我可以这样调用$('#BusyBox')。busyToggle(标志,0,1500,函数(){alert('complete…')});可能会有任何问题,因为我没有传递任何缓和争论的值?请详细讨论。。。thanks@Thomas:是的,您传递回调的方式与使用其他jQuery方法的方式完全相同。函数只是JavaScript中的值、链接字符串和数字,您可以用同样的方式对待它们。@Thomas将其添加到我的回答中。@U没有说明在我调用busyToggle()时如何附加回调函数……我可以这样调用$('BusyBox')。busyToggle(标志,0,1500,函数(){alert('complete…')});可能会有任何问题,因为我没有传递任何缓和争论的值?请详细讨论。。。thanks@Thomas:是的,您传递回调的方式与使用其他jQuery方法的方式完全相同。函数只是JavaScript中的值、链接字符串和数字,您可以用同样的方式对待它们。@Thomas将其添加到我的回答中。@U没有说明在我调用busyToggle()时如何附加回调函数……我可以这样调用$('BusyBox')。busyToggle(标志,0,1500,函数(){alert('complete…')});当我调用busyToggle()时,你没有说如何附加回调函数……我可以这样调用$(“#BusyBox”).busyToggle(flag,0,1500,function(){alert('complete…)});