我有两个容器,每个容器触发一些函数,它必须传递对象引用。 该函数具有嵌套的队列和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,但它们都指向同一个对象。
任何意见,赞赏。由于
_“的任何建议都赞赏” _ **不要传递字符串到'setTimeout'是我的建议... ** – gdoron
但我没有看到任何其他方式......我需要通过它...... :) – Anonymous
@ user1125062:'setTimeout'接受函数引用,所以'setTimeout(whatthe,400)'等价于'setTimeout(“whatthe()”,400)',但没有隐式调用'eval'。一般来说,你永远不需要在JavaScript中将实际的*代码*放在引号中。 –