2012-05-12 46 views
0

我有两个容器,每个容器触发一些函数,它必须传递对象引用。 该函数具有嵌套的队列和setTimeout,我需要通过它们两个来传递对象引用,以在正确的对象上执行它。如何将参数传递给队列_然后_到setTimeout?...混淆

这是我的尝试:

var pimg=parent.find('.prod_img'); // This is the object I actually need to pass; 

    pimg.children('img:last').queue((function(aaa){ 
     return function() { 
     whatthe= (function (itema) { 
       return function() { 
       itema.children('img').each(function(){ 
        alert(itema.attr('id')); 
       //alert used for debug. 
$(this).stop(false,false).animate({opacity:0},400,function(){ 
        }); 
       }); 
       } 
      })(aaa) 
     aaa.data('timeout',window.setTimeout("whatthe()", 400));  
     } 
    })(pimg) 
    ); 

现在发生的事情是,如果我赶紧触发我的两个对象的这一功能,它会提醒相同的ID,这表明它永远不会传递对象引用。

请注意,pimg是实际的对象,然后在队列引用中调用aaa,然后在setTimeout引用中调用itema,但它们都指向同一个对象。

任何意见,赞赏。由于

+3

_“的任何建议都赞赏” _ **不要传递字符串到'setTimeout'是我的建议... ** – gdoron

+0

但我没有看到任何其他方式......我需要通过它...... :) – Anonymous

+1

@ user1125062:'setTimeout'接受函数引用,所以'setTimeout(whatthe,400)'等价于'setTimeout(“whatthe()”,400)',但没有隐式调用'eval'。一般来说,你永远不需要在JavaScript中将实际的*代码*放在引号中。 –

回答

2

你的代码是由大脑伤害,我没有你的HTML来实际测试这一点,但如果我正确理解你的问题,这应该工作:

var pimg = parent.find('.prod_img'); // This is the object I actually need to pass; 

pimg.children('img:last').queue((function(aaa){ 
    return function() { 
     var whatthe = (function (itema) { 
      return function() { 
       itema.children('img').each(function(){ 
        alert(itema.attr('id')); 
        //alert used for debug. 
        $(this).stop(false,false).animate({opacity:0},400, function(){}); 
       }); 
      } 
     })(aaa) 
     aaa.data('timeout',window.setTimeout(whatthe, 400)); 
    } 
})(pimg)); 
+0

我不知道你做了什么,但它现在有效:D – Anonymous

+0

啊,我看,这是eval的问题,尽管对每个人都是! – Anonymous

+0

@ user1125062:好的,如果有效,请将答案标记为解决方案;-) –

相关问题