Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试筛选最近使用的数组项 Array.prototype.spil=函数(x,最大值){ if(this.length==max){ 这个。shift(); 这个。推(x); }否则{ 这个。推(x); } 归还这个; } var heard=[0], 弗雷什=[]; $('#speak').bind('touchstart',function()){ 变量声音=[ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40 ]; var-temp=声音; 对于(var i=0;i_Javascript_Arrays - Fatal编程技术网

Javascript 尝试筛选最近使用的数组项 Array.prototype.spil=函数(x,最大值){ if(this.length==max){ 这个。shift(); 这个。推(x); }否则{ 这个。推(x); } 归还这个; } var heard=[0], 弗雷什=[]; $('#speak').bind('touchstart',function()){ 变量声音=[ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40 ]; var-temp=声音; 对于(var i=0;i

Javascript 尝试筛选最近使用的数组项 Array.prototype.spil=函数(x,最大值){ if(this.length==max){ 这个。shift(); 这个。推(x); }否则{ 这个。推(x); } 归还这个; } var heard=[0], 弗雷什=[]; $('#speak').bind('touchstart',function()){ 变量声音=[ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20, 21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40 ]; var-temp=声音; 对于(var i=0;i,javascript,arrays,Javascript,Arrays,我认为这条线 Array.prototype.spil = function (x,max) { if (this.length===max) { this.shift(); this.push(x); } else { this.push(x); } return this; } var heard = [0], freash = []; $('#speak').bind('touchstart', f

我认为这条线

Array.prototype.spil = function (x,max) {
    if (this.length===max) {
        this.shift();
        this.push(x);
    } else {
        this.push(x);
    }
    return this;
}

var heard = [0],
    freash = [];

$('#speak').bind('touchstart', function() {
    var sounds = [
        1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
        21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
    ];
    var temp = sounds;
    for (var i=0; i<heard.length; i++) {
        temp.splice(temp.indexOf(heard[i]), 1);
    }
    freash = temp;
    var say = Math.floor(Math.random() * freash.length+1) + 1;
    heard.spil(say,10);
    say = document.getElementById('c'+say); // audio element
    // use the sound...
});
应该是

var say = Math.floor(Math.random() * freash.length+1) + 1;

也可考虑捷径

var say = freash[Math.floor(Math.random() * freash.length)];

好的,+1代表非常好的捷径!为什么是那一行?哪里错了???@8DK你的代码的问题是你使用了数组的随机键,而不是它对应的值。例如,假设你第一次得到
5
,那么下次你将得到
[1,…,4,6,…,40]
。但是,不是得到这些数字中的一个,而是得到一个介于
2
40
之间的数字。下一次,在
2
39
2
38
之间,等等。因此,您不必阻止再次选择最近的数字,而是排除高数字。感谢您的澄清
var say = freash[Math.random() * freash.length | 0];