buttonを押した時にclearTimeoutを使ってstartSlot()を停止させたいのですが出来ません。どうしたらいいでしょうか?
さらになんですが、これをbuttomを押した時にゆっくりと止まる方法を教えていただきたいです。
HTML
1<!DOCTYPE html> 2<meta charset = 'utf-8'> 3<script src ='jquery-1.12.4.js'></script> 4<script src='slot2.js'></script> 5 6<body> 7 <div class='container'> 8 <div id="slot"> 9 <div id='box1' class='box'>0</div> 10 <div id='box2' class='box'>0</div> 11 <div id='box3' class='box'>0</div> 12 </div> 13 14 <button class='start'>start</button> 15 <div id='answer'>good!</div> 16 17 </div> 18</body> 19
JavaScript
1$(function(){ 2 3 var start_count = 0; 4 var speed = 0; 5 var x,y,z; 6 var id = setTimeout(startSlot, 100); 7 8 $('.start').click(function(){ 9 if(start_count == 0){ 10 startSlot(); 11 start_count += 1; 12 }else if(start_count == 1){ 13 clearTimeout(id); 14 start_count == 0; 15 } 16 }); 17 18 19 var startSlot = function(){ 20 21 console.log(speed++); 22 var x = Math.floor(Math.random() * 10); 23 var y = Math.floor(Math.random() * 10); 24 var z = Math.floor(Math.random() * 10); 25 26 $('#box1').text(x); 27 $('#box2').text(y); 28 $('#box3').text(z); 29 setTimeout(startSlot,100); 30 } 31}); 32
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。