Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中将匿名函数作为参数传递_Javascript - Fatal编程技术网

在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

我有以下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.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);