无法在丢弃时获得正确的元素数

时间:2013-10-01 15:20:25

标签: javascript jquery jquery-ui

我注意到在多次尝试获取丢弃元素的数量时,它总是返回的数量少于找到的数量,但如果我通过Chromes控制台尝试相同的命令,我会得到propper响应。

我认为当这个脚本运行时DOM没有更新,所以我抛出了超时/再试机制,但仍无济于事。我错过了什么?

预期结果尺寸= 10

实际结果尺寸= 9

代码

var asmCount = 0;

function storeValues(values, tryNumber){
    var size = values.length,
        tryNumber = (!isNaN(tryNumber)) ? tryNumber : 1
        ;

    console.log('full boat:' + size + ', try #'+ tryNumber + ' ' + typeof(tryNumber));
    if(tryNumber > 60){
        console.log('too many tries');
        return;
    }

    if(size < 10){
        console.log('missing entries');
        tryNumber = tryNumber + 1;
        setTimeout(storeValues($('.asm-ranking .receiver .asm-value'), tryNumber) , 500);
    } else if(size > 10){
        console.log('too many entries, something broke');
    } else {
        console.log('just right, lets do this shit.');
        //$.each(values, function(k,v){ console.log($(v).data('id'));});
    }   
}

$('.asm-ranking .receiver').droppable({
    drop: function(event,ui){
        var _self = this,
            receiverCount = $('.asm-ranking .receiver li').length
        ;
        //console.log('event',event);
        //console.log('ui', ui);
        asmCount++;
        console.log(asmCount + ' / ' +  receiverCount);
        if(receiverCount == asmCount){
            storeValues($('.asm-ranking .receiver .asm-value'), 1); // give DOM time to catch up, then store
        }
    }
});

1 个答案:

答案 0 :(得分:0)

由于某种原因,我将jQuery表示法包含在文档准备好的

$(document).ready(function{ ... })

它按预期工作。