[英]Stop array from being overwritten or called multiple times
I am trying to remove items from an array which I have successfully done but am not getting desired results. 我正在尝试从成功完成的数组中删除项目,但未获得期望的结果。 Every time a div with a unique id is clicked it should remove that unique id from the array squaresNotClicked
and when I output to console
I want it to show me the array one time instead of repeating itself. 每次单击具有唯一ID的div时,都应从数组squaresNotClicked
删除该唯一ID,当我输出到console
我希望它向我显示该数组一次,而不是重复自身。 I have included my code and the output below. 我在下面包括了我的代码和输出。
$("#board").on("click", ".boardSquares:not('.clicked')", function( event ) {
$(this).addClass('clicked');
var squaresNotClicked = ['1', '2', '3', '4', '5', '6', '7', '8' ,'9'];
var squaresClicked = new Object();
var square = event.target.id;
if(this.className != 'clicked') {
var removeSquare = "" + square + "" ;
$(this).prepend('<img class="theImg" src="images/X.png" />');
squaresClicked[square] = '1' ;
squaresNotClicked = jQuery.grep(squaresNotClicked, function(value) {
return value != removeSquare;
});
}
function checkArray() {
jQuery.each( squaresClicked, function(i, val) {
console.log(squaresClicked);
});
}
$("body").dblclick(function() {
checkArray();
console.log(squaresNotClicked);
});
});
In this example output I clicked square 2 and 3 then double clicked to output the contents of the array to the console. 在此示例输出中,我单击了正方形2和3,然后双击将数组的内容输出到控制台。 I understand that the array is being reassigned the with the starting values each time. 我了解每次都会使用起始值重新分配数组。
Object {2: "1"}
["1", "3", "4", "5", "6", "7", "8", "9"]
Object {3: "1"}
["1", "2", "4", "5", "6", "7", "8", "9"]
If I move the line where I create the array var squaresNotClicked
outside of the scope of this function and click squares 2 and 3 again I get this for output: 如果我将创建数组var squaresNotClicked
的行移到此函数范围之外,然后再次单击平方2和3, var squaresNotClicked
得到以下结果:
Object {2: "1"}
["1", "4", "5", "6", "7", "8", "9"]
Object {3: "1"}
["1", "4", "5", "6", "7", "8", "9"]
This is close to what I want but it will keep outputting the array to console for however many times I click a square. 这接近我想要的,但是无论我单击一个正方形多少次,它都会继续将阵列输出到控制台。 If I click squares 1, 2, 3 it will output the array to console 3 times. 如果单击正方形1、2、3,它将把数组输出到控制台3次。 How do I prevent this from occurring? 如何防止这种情况发生?
To prevent the multiple showing of the result in the console put the following code outside of the click handler: 为了防止在控制台中多次显示结果,请将以下代码放在点击处理程序之外:
$("body").dblclick(function() {
checkArray();
console.log(squaresNotClicked);
});
Once you click on .boardSquares
you attach a new listener to the body. 单击.boardSquares
,将新的侦听器附加到主体。
It's not overwritten actually, you should say used only one time. 它实际上并没有被覆盖,您应该说只使用了一次。 Keep in mind that each time you click on a square, the entire event handler is called again and again. 请记住,每次单击正方形时,都会一次又一次调用整个事件处理程序。 Put everything which does not need to be redefined outside of it. 将不需要重新定义的所有内容放在外部。 Here is a little help (not tested, I let you check by yourself) : 这里有一些帮助(未经测试,我让您自己检查):
var squaresNotClicked = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
var squaresClicked = new Object();
$("#board").on("click", ".boardSquares:not('.clicked')", function (event) {
$(this).addClass('clicked');
var square = event.target.id;
if (this.className != 'clicked') {
var removeSquare = "" + square + "";
$(this).prepend('<img class="theImg" src="images/X.png" />');
squaresClicked[square] = '1';
squaresNotClicked = jQuery.grep(squaresNotClicked, function (value) {
return value != removeSquare;
});
}
});
function checkArray() {
jQuery.each(squaresClicked, function (i, val) {
console.log(squaresClicked);
});
}
$("body").dblclick(function () {
checkArray();
console.log(squaresNotClicked);
});
Change the body dubble click event : 更改主体Dubble Click事件:
$("body").dblclick(function() {
console.log(squaresNotClicked);
});
Also, moveit outside of the "main" click event! 另外,将Moveit置于“主要”点击事件之外!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.