2016-02-11 33 views
0

我有点棒球比赛,显然没有基础或命中。它的所有描述,球和出局。按钮的工作和局的功能(顶部和底部以及计数)正在工作。我希望这可以每隔几秒钟完全随机地抛出一个球。基本上随机“点击”球或罢工按钮。我正在尝试使用setTimeout函数,但没有成功。任何帮助?自动播放功能在JS中不执行

HTML:

<!DOCTYPE HTML> 
<html> 
    <head> 
     <link rel="stylesheet" type="text/css" href="bullpen.css"> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Bullpen</title> 
    </head> 
</html> 
    <body> 

     <button id=buttons onclick="playBall()">Auto-Play</button> 
     <h2>Inning: <span id=inningHalf></span> <span id=inningNum></span></h2> 

    <div id=buttons> 
     <button onclick="throwBall(), newInning()">Ball</button> 
     <button onclick="throwStrike(), newInning()">Strike</button> 
    </div> 

     <h2>Count: <span id=ball>0</span> - <span id=strike>0</span></h2> 
     <h2>Outs: <span id=out>0</span></h2> 

     <h2>----------------</h2> 

     <h2>Walks: <span id=walks>0</span></h2> 
     <h2>Strikeouts: <span id=strikeout>0</span></h2> 

     <script src="bullpen.js"></script> 
    </body> 
</html> 

JS:

var ball = 0; 
var strike = 0; 
var out = 0; 
var walks = 0; 
var strikeout = 0; 
//--------------------------------- 
var outPerInning = 0; 
var inning = 1; 
//--------------------------------- 
document.getElementById('inningNum').innerHTML = inning; 
document.getElementById('inningHalf').innerHTML = '▲'; 

function throwBall(){ 
    ball++; 
    document.getElementById('ball').innerHTML = ball; 

    if (ball == 4){ 
    ball = 0; 
    strike = 0; 
    walks++; 

    document.getElementById('walks').innerHTML = walks; 
    document.getElementById('strike').innerHTML = 0; 
    document.getElementById('ball').innerHTML = 0; 
    } 
}; 

function throwStrike(){ 
    strike++; 
    document.getElementById('strike').innerHTML = strike; 

    if(strike == 3){ 
    ball = 0; 
    strike = 0; 
    strikeout++; 

    out++; 
    outPerInning++; 

    document.getElementById('strikeout').innerHTML = strikeout; 
    document.getElementById('out').innerHTML = out; 
    document.getElementById('strike').innerHTML = 0; 
    document.getElementById('ball').innerHTML = 0; 
    } 
}; 


function newInning(){ 
    if(out == 3){ 
    ball=0; 
    strike=0; 
    out=0; 

    document.getElementById('strike').innerHTML = 0; 
    document.getElementById('ball').innerHTML = 0; 
    document.getElementById('out').innerHTML = 0; 
    } 

    if(outPerInning == 3){ 
    document.getElementById('inningHalf').innerHTML = '▼'; 
    } 

    if(outPerInning == 6){ 
     inning++; 
    document.getElementById('inningHalf').innerHTML = '▲'; 
     outPerInning = 0; 
    document.getElementById('inningNum').innerHTML = inning; 
    } 
}; 

function playBall(){ 
    var play = Math.random; 
    if(play >= 0.4){ 
     throwStrike(); 
     newInning(); 
    } 
    else{ 
     throwBall(); 
     newInning(); 
    } 

    setTimeout(playBall, 5000); 
}; 

CSS如果需要的话:

h2{ 
    margin-left: 50px; 
    font-size: 50px; 
} 

button{ 
    font-size: 45px; 
} 

#buttons{ 
    width: 227px; 
    margin-left:50px; 
    margin-top: 20px; 
} 
+0

我已经编辑我的答案,包括工作的jsfiddle –

回答

0

也许这就是问题所在:

function playBall(){ 
    var play = math.random; 
    if(play >= 0.4){ 
     throwStrike; 
     newInning(); 
    } 
    else{ 
     throwBall; 
     newInning(); 
    } 

    setTimeout(playBall, 5000); 
}; 

function playBall(){ 
    var play = Math.random(); 
    if(play >= 0.4){ 
     throwStrike(); //call the function 
     newInning(); 
    } 
    else{ 
     throwBall(); //call the function 
     newInning(); 
    } 

    setTimeout(playBall, 5000); 
}; 

你没有调用这些函数!

这里是工作提琴:https://jsfiddle.net/av6vsp7g/

+0

谢谢!更改我的代码后,我忘了按下按钮。万分感谢。 –