所以这里是代码段
$(document).ready(function () {
var dmg = 60;
var curr_hp = 1200;
var tot_hp = 1200;
$('#attk_spd').text('1.2 seconds');
$('#dmg').text('60');
$('#curr_hp').text('1200');
$('#tot_hp').text('1200');
var dps_timer;
var attacking = 0;
$("#btn").click(function () {
if (dps_timer) {
// We're already waiting for the next attack, so do nothing
return;
}
$('#attk').text('You are currently attacking your target.');
$('#btn').val('Retreat');
var attack_once = function () {
// Always attack immediately
curr_hp -= dmg;
$('#curr_hp').text(curr_hp);
if (curr_hp > 0) {
dps_timer = setTimeout(attack_once, 1200);
} else if (attacking == 1) {
clearTimeout(attack_once);
attacking = 0;
alert(attacking);
$('#attk').text('You have retreated from battle.');
$('#btn').val('Attack');
} else {
attacking = 0;
alert(attacking);
$('#attk').text('Your target has been destroyed.');
$('#btn').val('Attack');
}
}
if (attacking == 0) {
attack_once();
attacking = 1;
alert(attacking);
}
});
});
我设置了警报以检查值是否正在发生变化。这基本上是一个按钮点击功能。因此,当我第一次单击按钮时,攻击开始,按钮变为撤退。但是当我单击撤退时,应该再次执行该功能检查条件,攻击变量最初设置为0,单击攻击时更改为1。因此,如果您再次点击攻击,它将运行第二个“否则”如果'有条件的,但它没有。救命啊!
此处的操作代码:http://www.evenstar-online.com/Jquery_snippets/dpsloop.php
答案 0 :(得分:0)
attacking
时, attack_once
永远不能为1,因为您的if
语句要求0
运行{。}}。
if (attacking == 0) {
//attacking is 0 when `attack_once()` is called
attack_once();
attacking = 1;
alert(attacking);
}
var attack_once = function () {
//.....
// this can never be 1
} else if (attacking == 1) {
解决方案:将attacking = 1
移到attack_once();
行
if (attacking == 0) {
attacking = 1;
attack_once();
alert(attacking);
}