Javascript jquery-如何在一个函数返回值后继续执行另一个函数
我目前有一个名为showAuthorInfo的函数,它执行动画,然后在动画完成后执行一些其他代码逻辑:Javascript jquery-如何在一个函数返回值后继续执行另一个函数,javascript,jquery,callback,return,promise,Javascript,Jquery,Callback,Return,Promise,我目前有一个名为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'); s
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,即使动画函数仅在showAuthorInfo调用时才需要authorID)
这里有什么建议吗
我不想做的事情的例子:
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
})
}
您只需传递回调即可设置动画并避免不必要的函数换行:
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,function(){
//do something here
});
}
self.someOtherFunction = function(){
var authorID2 = $authorLink2.attr('data-author-id2'); //PL-INT - determine what info needs be captured here
var callback2 = self.displayAuthorContent2();
self.animateInfoWindow(authorID2,function(){
//do something different
});
}
self.animateInfoWindow = function(authorID,callback){
self.$modalInfoWindow.animate({ 'bottom': '0px' },200,callback)
}
这是一把小提琴:
我真的不知道你想通过以下方式实现什么:
var callback = self.displayAuthorContent();
调用函数之前先调用回调函数,这是回调函数的全部用途。可能只是函数的名称
var
iable如果您想让动画尽可能通用,那么只需要一个回调函数,而不需要参数。回调函数可以更具上下文意识,以允许灵活性
self.showAuthorInfo = function(){
var authorID = $authorLink.attr('data-author-id');
var customCallback = function(){
self.loadAuthorInfo(authorID);
};
self.animateInfoWindow(customCallback);
}
self.animateInfoWindow = function(callback){
self.$modalInfoWindow.animate({ 'bottom': '0px' },200,callback);
}
Mheavers,您已将您的问题标记为
promise
,这意味着您的思路是正确的
也许很少有人理解jQuery允许从jQuery集合中形成特殊承诺,使用$(选择器)。承诺(类型,目标)
,通常位于较长方法链的末尾。类型
默认为“fx”(标准动画队列),目标是可选的(此处不相关)
因此,self.animateInfoWindow
可以很高兴地不知道在它启动的动画完成后会发生什么。它所需要做的就是返回上述类型的承诺,从而允许在调用self.animateInfoWindow
的地方进行链接
self.animateInfoWindow = function() {
return self.$modalInfoWindow.animate({'bottom':'0px'}, 200).promise();
};
而self.showAuthorInfo
可以执行动画后期操作,而无需将任何参数传递给self.animateInfo窗口
self.showAuthorInfo = function() {
var authorID = $authorLink.data('authorId') || null; //PL-INT - determine what info needs be captured here
self.animateInfoWindow().done(function() {
self.$modalInfoWindow.addClass('active');
self.loadAuthorInfo(authorID);
});
};
注意,第一行中的| | null
,以确保authorID
不是未定义的(如果authorID为0(零)是有效的,则稍微复杂一些)
唯一的另一件事是确保self.loadAuthorInfo
不能被传递null
showAuthorInfo
是一个函数调用还是一个函数声明?看起来你正在调用并声明它。你能提供更多的代码吗?很好!这正是我想要做的,只是不确定该怎么做w要构造it.mh,再考虑一下;使用此结构,您可以在.done()中移动行var authorID=…
handler,从而避免了self.showAuthorInfo
形成闭包的需要,尽管这样做意味着authorID
在动画开始之前不会被锁定。对于200毫秒的胺化持续时间,这两种方式可能都不是什么大问题。