Jquery随机数组删除

时间:2013-06-04 11:41:17

标签: jquery arrays function random numbers

var items = ["1","2","3","4","5","6","7","8","9","10","11","12"];
function bigger(){
    for(var i=0;i<items.length;i++){
        a = [Math.floor(Math.random() * items.length)+1];
        scale(a);   
    }   
}
function scale(number){
    $("#inner"+number+"").delay(100).transition({scale:1},300);
    items.splice(number,1);
} 
bigger();

这是我的代码ı尝试删除数字并使用,直到完成数组,我想1做1

2 个答案:

答案 0 :(得分:0)

您需要数组中splice()的数字索引,而不是字符串本身:

items.splice( items.indexOf(number), 1 );

可能更容易将数组洗牌并弹出最后一个值以获取随机数组索引,同时将其从数组中删除:

var items = ["1","2","3","4","5","6","7","8","9","10","11","12"];

function scale(){
    var number = items.sort(function() { return 0.5 - Math.random();}).pop();
    $("#inner"+number+"").delay(100).animate({opacity:1},300, function() {
        scale();
    });
} 

scale();

不确定.transition()方法的来源,但猜测它有回调?

FIDDLE

答案 1 :(得分:0)

你需要

items.splice($.inArray(number, items),1);