【发布时间】:2013-03-31 01:44:59
【问题描述】:
我目前有一个名为 showAuthorInfo 的函数,它执行动画,然后在动画完成后执行一些其他代码逻辑:
self.showAuthorInfo = function(){
var authorID = $authorLink.attr('data-author-id'); //PL-INT - determine what info needs be captured here
var isActive = $modalInfoWindow.hasClass('active');
self.$modalInfoWindow.animate({ 'bottom': '0px' },200,function(){
self.$modalInfoWindow.addClass('active');
self.loadAuthorInfo(authorID);
})
}
但是,因为我想通过各种函数调用来显示和隐藏这个模态窗口,每次动画完成时执行不同的回调,我想将动画包装成一个函数。问题是,我可以使用上面的函数在动画发生的地方调用一个自定义函数,并让那个动画函数返回一个值给这个函数,然后它可以继续吗?
我会将函数分解为多个函数,但我觉得这可能会变得复杂,尤其是因为我必须传递某些不适用于所有情况的特定于函数的参数(例如,在代码中)上面,如果我要调用一个动画函数,然后是一个 loadAuthorInfo 函数,动画函数必须接受 authorID,以便它可以将它传递给 loadAuthorInfo,即使动画函数只需要 authorID,如果它被 showAuthorInfo 调用) .
这里有什么建议吗?
我不想做的例子:
self.showAuthorInfo = function(){
var authorID = $authorLink.attr('data-author-id'); //PL-INT - determine what info needs be captured here
var callback = self.displayAuthorContent();
self.animateInfoWindow(authorID,callback);
}
self.animateInfoWindow = function(authorID,callback){
self.$modalInfoWindow.animate({ 'bottom': '0px' },200,function(){
//create conditional where if there's an authorID, pass it to the callback function, otherwise just call the callback function without parameters
})
}
【问题讨论】:
-
showAuthorInfo是函数调用还是函数声明?看来您正在调用它并声明它。能否提供更多代码?
标签: javascript jquery callback return promise