Skip to content

Instantly share code, notes, and snippets.

@backy22
Created June 9, 2015 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save backy22/6f3b62a286151284ec55 to your computer and use it in GitHub Desktop.
Save backy22/6f3b62a286151284ec55 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>スロットマシーン</title>
</head>
<style>
.col{
float: left;
width:100px;
text-align: center;
font-size:32px;
}
</style>
<body>
<div class="col">
<span id="num0">?</span><br>
<input type="button" value="STOP" id="stop0">
</div>
<div class="col">
<span id="num1">?</span><br>
<input type="button" value="STOP" id="stop1">
</div>
<div class="col">
<span id="num2">?</span><br>
<input type="button" value="STOP" id="stop2">
</div>
<script>
(function(){
var timers,
nums,
stopCount;
document.getElementById('stop0').onclick = function(){
stopSlot(0);
};
document.getElementById('stop1').onclick = function(){
stopSlot(1);
};
document.getElementById('stop2').onclick = function(){
stopSlot(2);
};
function starSlot(){
timers = [];
nums = [];
stopCount = 0;
runSlot(0);
runSlot(1);
runSlot(2);
}
function stopSlot(n){
if (typeof nums[n] !== 'undefined'){
return;
}
clearTimeout(timers[n]);
nums[n] = document.getElementById('num'+n).innerHTML;
stopCount++;
if (stopCount == 3){
checkSlot();
}
}
function checkSlot(){
nums.sort();
console.log(nums);
if (nums[0] == nums[1] && nums[0] == nums[2]){
alert('全部揃った');
} else if(nums[0] == nums[1] || nums[1] == nums[2]){
alert('2つ揃った');
} else {
alert('残念');
}
}
function runSlot(n){
document.getElementById('num'+n).innerHTML = Math.floor(Math.random()*10);
timers[n] = setTimeout(function(){
runSlot(n);
}, 50);
}
starSlot();
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment