0

我有两个容器,每个都触发一些函数,它必须传递对象引用。该函数具有嵌套队列和 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,但它们都应该指向同一个对象。

任何建议表示赞赏。谢谢

4

1 回答 1

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));
于 2012-05-12T22:18:52.130 回答