【发布时间】:2020-05-22 08:26:14
【问题描述】:
所以我们开始吧,我有 2 个按钮可以在它们之间切换,我想确保在再次绑定之前删除点击以避免垃圾邮件。按钮会一直切换,除非在 playRound 点击我们得到一个 JSON 告诉我们否则。
我希望在他点击之后,在我再次将按钮与下一个按钮绑定之前,它无法再次执行此操作。
这就是我尝试过的:
“NextStage”按钮绑定函数
function bindNextStage() {
$('#nextStage').show().click(function () {
let $pkEnemy = document.getElementById('pkEnemy').innerText;
$('#nextStage').hide().click(false);
$.ajax({
url: '/nextEnemy/' + $pkEnemy,
type: 'get',
dataType: 'json',
}).done(function (result) {
console.log("refresh some data");
bindPlayRound();
});
});
}
绑定playRound的函数
function bindPlayRound($pkParty) {
$('#playRound').show().click(function () {
let $pkEnemy = document.getElementById('pkEnemy').innerText;
$('#playRound').click(false).hide();
$.ajax({
url: '/playRound/' + $pkEnemy,
type: 'get',
dataType: 'json',
}).done(function (result) {
addBattleReport(result).then(r => {
console.log("do something with an other button")
});
console.log("refresh some data")
if (result['isEnded'])
console.log('END');
else if (result['enemy']['hp'] <= 0) {
bindNextStage();
}
else{
bindPlayRound()
}
});
});
}
我要求开始
bindNextStage()
----编辑----
目前,如果我按 F12 并显示该按钮,我可以再次单击该按钮,但除此之外,该功能还会启动 3 次。当我单击该按钮时,它隐藏得很好,但它没有解除绑定,并且在启动 3 次时有一个奇怪的行为。
----编辑2---
我知道“.click”函数不会被 .click(false) 解除绑定这一事实会像绑定它一样多次启动我的函数。
【问题讨论】:
-
你能更详细地写出你所面临的错误吗?以便人们了解问题
-
目前,如果我执行 F12 并显示该按钮,我可以再次单击该按钮,但除此之外,该功能还会启动 3 次。当我点击该按钮时,它隐藏得很好,但它没有解除绑定,并且在启动 3 次时有一个奇怪的行为。
-
感谢您的补充。在下面检查我的答案。
标签: javascript json button binding