在使用jQuery來管理Slot Machine(老虎機)的數據時,你可以采用以下方法:
var slotMachine = {
wheels: [
['A', 'B', 'C'],
['D', 'E', 'F'],
['G', 'H', 'I']
],
currentWheelIndex: 0,
currentResult: ''
};
function spinWheel() {
slotMachine.currentWheelIndex = (slotMachine.currentWheelIndex + 1) % slotMachine.wheels.length;
slotMachine.currentResult = slotMachine.wheels[slotMachine.currentWheelIndex][Math.floor(Math.random() * slotMachine.wheels[slotMachine.currentWheelIndex].length)];
}
<div id="wheel"></div>
$('#wheel').text(slotMachine.wheels[slotMachine.currentWheelIndex][0]);
spinWheel
函數并更新顯示。<button id="spin">Spin</button>
$('#spin').click(function() {
spinWheel();
$('#wheel').text(slotMachine.wheels[slotMachine.currentWheelIndex][0]);
});
function resetMachine() {
slotMachine.currentWheelIndex = 0;
slotMachine.currentResult = '';
$('#wheel').text(slotMachine.wheels[slotMachine.currentWheelIndex][0]);
}
function showResult() {
alert('Your result is: ' + slotMachine.currentResult);
}
通過這種方式,你可以使用jQuery來管理Slot Machine的數據和交互。當然,這只是一個簡單的示例,你可以根據需要擴展和優化代碼。