在javascript中将匿名函数作为参数传递
我有以下javascript代码:在javascript中将匿名函数作为参数传递,javascript,Javascript,我有以下javascript代码: EventsManager.prototype.hideContainer = function() { var that = this; var index = that.getNextUnreadEventIndex(); if(index !== -1) { EventsManager.animateHideLeft(function() //<--- passing a fu
EventsManager.prototype.hideContainer = function()
{
var that = this;
var index = that.getNextUnreadEventIndex();
if(index !== -1)
{
EventsManager.animateHideLeft(function() //<--- passing a function as parameter to another function
{
var unreadEvent = that.eventsList.splice(index,1)[0];
unreadEvent.isEventOnFocus = true;
that.eventsList.push(unreadEvent);
that.displayLastEvent();
});
}
}
EventsManager.prototype.hideContainer=function()
{
var=这个;
var index=that.getNextUnreadEventIndex();
如果(索引!=-1)
{
EventsManager.animateHideLeft(function()//您在其他位置缺少回调
fr = setTimeout(function()
{
EventsManager.animateHideLeft(function(){
////
});
}, 50);
看起来您只需要在setTimeout
中通过调用传递callback
fr = setTimeout(function()
{
EventsManager.animateHideLeft(callback);
}, 50);
这是因为您从setTimeout()
中错误地调用了它:
在setTimeout
中,您不会将回调传递给下一次调用:
EventsManager.animateHideLeft();
换成
EventsManager.animateHideLeft(callback);
但是,针对typeof callback==“function”
进行测试并不是一个坏主意,因为有时您不想要/不需要回调函数,callback();
调用会导致异常
顺便说一句,您不需要clearTimeout(fr);
(除非您计划在动画中多次调用该函数)。除了所问的问题之外,请仔细查看如果(!width | width==“NaN”)width=200;
。确保它符合您的预期。
EventsManager.animateHideLeft();
EventsManager.animateHideLeft(callback);