Javascript-setInterval,如果选中复选框,则交替选中收音机(无库)
我想用单选按钮将行走显示为复选框。每2秒钟,角色开始移动的概率为30%,停止移动的概率为30%。 如果选中该复选框,则表示角色正在移动。 第二个和第三个单选按钮显示角色前面的左腿或右腿。我知道在代码中添加多个setTimeout是一个很大的错误 所以我想做的是:如果选中复选框,单选按钮2、3应该交替更改其值。如果未选中,则选中第一个单选按钮 他在搬家吗?[]腿:* 他在搬家吗?[✓] 腿:|* 他在搬家吗[✓] 腿:|* 他在搬家吗[✓] 腿:|*Javascript-setInterval,如果选中复选框,则交替选中收音机(无库),javascript,animation,checkbox,radio-button,setinterval,Javascript,Animation,Checkbox,Radio Button,Setinterval,我想用单选按钮将行走显示为复选框。每2秒钟,角色开始移动的概率为30%,停止移动的概率为30%。 如果选中该复选框,则表示角色正在移动。 第二个和第三个单选按钮显示角色前面的左腿或右腿。我知道在代码中添加多个setTimeout是一个很大的错误 所以我想做的是:如果选中复选框,单选按钮2、3应该交替更改其值。如果未选中,则选中第一个单选按钮 他在搬家吗?[]腿:* 他在搬家吗?[✓] 腿:|* 他在搬家吗[✓] 腿:|* 他在搬家吗[✓] 腿:|* 我会这样设置。有一个间隔大约每2秒运行一次,以
我会这样设置。有一个间隔大约每2秒运行一次,以确定是否步行。一旦步行开始,第二个间隔大约每0.5秒移动一步 第一个间隔保持每2秒运行一次,如果它决定不行走,则取消第二个间隔并停止行走。如果稍后它决定再次行走,则再次开始第二个间隔 希望这些评论能把事情说清楚
<script>
var walk = (function() {
// Keep variables in a closure rather than global
var moveBox, leg, m, walkInterval, i=0;
return {
init: function () {
// Do setup, run every 2 seconds
moveBox = document.getElementById('movement');
leg = document.getElementsByName('legs');
walk.stop();
window.setInterval(function(){walk.start()}, 2000);
},
start: function () {
m = Math.random() * 10 | 0; // truncate for convenience
// about 30% of the time, walk
if (m < 3) {
// If not already walking, start stepping every .5 seconds
if (!walkInterval) {
walkInterval = setInterval(function(){walk.move()},500);
console.log('starting to walk…');
}
// 60% of the time, don't walk
} else {
walk.stop()
console.log('stopped');
}
},
move: function () {
// If already moving, swap legs
if (moveBox.checked) {
leg[1].checked = !leg[1].checked;
leg[2].checked = !leg[2].checked;
// Otherwise, set to initial moving condition
} else {
moveBox.checked = true;
leg[0].checked = false;
leg[1].checked = true;
leg[2].checked = false;
}
},
stop: function () {
// Clear the interval if it's running
if (walkInterval) {
window.clearInterval(walkInterval);
walkInterval = null;
}
// Set checkboxes to stopped condition
moveBox.checked = false;
leg[0].checked = true;
leg[1].checked = false;
leg[2].checked = false;
}
}
}());
window.onload = walk.init;
</script>
<table>
<tr>
<td>Is moving?<input type="checkbox" id="movement">
<td>Legs<input type="checkbox" name="legs">|<input type="checkbox" name="legs">
<input type="checkbox" name="legs">
因此,如果选中了移动,那么每0.5秒您就希望腿交换一次?在我看来,您需要摆脱设置超时并切换腿的检查状态。如果未选中移动,您还需要取消间隔,以便在可能修改其值时,每2秒您就可以开始和停止行走。
<script>
var walk = (function() {
// Keep variables in a closure rather than global
var moveBox, leg, m, walkInterval, i=0;
return {
init: function () {
// Do setup, run every 2 seconds
moveBox = document.getElementById('movement');
leg = document.getElementsByName('legs');
walk.stop();
window.setInterval(function(){walk.start()}, 2000);
},
start: function () {
m = Math.random() * 10 | 0; // truncate for convenience
// about 30% of the time, walk
if (m < 3) {
// If not already walking, start stepping every .5 seconds
if (!walkInterval) {
walkInterval = setInterval(function(){walk.move()},500);
console.log('starting to walk…');
}
// 60% of the time, don't walk
} else {
walk.stop()
console.log('stopped');
}
},
move: function () {
// If already moving, swap legs
if (moveBox.checked) {
leg[1].checked = !leg[1].checked;
leg[2].checked = !leg[2].checked;
// Otherwise, set to initial moving condition
} else {
moveBox.checked = true;
leg[0].checked = false;
leg[1].checked = true;
leg[2].checked = false;
}
},
stop: function () {
// Clear the interval if it's running
if (walkInterval) {
window.clearInterval(walkInterval);
walkInterval = null;
}
// Set checkboxes to stopped condition
moveBox.checked = false;
leg[0].checked = true;
leg[1].checked = false;
leg[2].checked = false;
}
}
}());
window.onload = walk.init;
</script>
<table>
<tr>
<td>Is moving?<input type="checkbox" id="movement">
<td>Legs<input type="checkbox" name="legs">|<input type="checkbox" name="legs">
<input type="checkbox" name="legs">