我有以下 javascript 代码:
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.animateHideLeft() 函数的代码:
EventsManager.animateHideLeft = function(callback)
{
var p = document.getElementById("eventsContainer");
var width = parseFloat(p.style.width);
if(!width || width === "NaN") width = 200;
if(width <= 10)
{
clearTimeout(fr);
alert(typeof callback); //<-- this shows "undefined"
callback();
}
else
{
width = width - 10;
p.style.width = width + "px";
fr = setTimeout(function()
{
EventsManager.animateHideLeft();
}, 50);
}
};
不幸的是,函数 animateHideLeft 没有按预期工作。当我测试 typeof 回调时,它会提醒“未定义”。
我怎样才能解决这种混乱,所以我得到预期的结果?