[英]Run jQuery click function ONCE
I'm having trouble getting this click function to run one single time per turnRound() call. 我在让此click函数每次turnRound()调用运行一次时遇到麻烦。 So I put it in an if-statement that should get broken after one execution, due to the humanMoved variable being true.
因此,我将其放入一个if语句中,由于humanMoved变量为true,因此该语句应在一次执行后中断。 But clicking on an element with the "square" class still calls the click function.
但是,单击具有“ square”类的元素仍然会调用click函数。
turnRound()
is called only once as well. turnRound()
也仅被调用一次。 I'm thinking this might be a variable scope problem with humanMoved, or I just don't understand .click()
我认为这可能是humanMoved的可变范围问题,或者我只是不理解
.click()
function turnRound(c){
humanMoved = false;
if (c == "X" && humanMoved == false) {
$(".square").click(function(){
$(this).text(c);
humanMoved = true;
});
}
Instead of the following code: 代替以下代码:
$(".square").click(function(){
$(this).text(c);
humanMoved = true;
});
Replace it with either .one()
: 用
.one()
替换它:
$(".square").one("click", function(){
$(this).text(c);
humanMoved = true;
});
Or, you can remove the click
event by using .off()
: 或者,您可以使用
.off()
删除click
事件:
$(".square").click(function(){
$(this).text(c);
humanMoved = true;
$(this).off("click");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.