我有两个容器,每个容器触发一些函数,它必须传递对象引用。 该函数具有嵌套队列和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,但它们都被指向同一个对象。
任何建议表示赞赏。感谢
答案 0 :(得分: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));