好的,所以我一直在做一些JS练习,我决定在控制台上进行篮球模拟。对我来说,主要问题是在功能之间制定时间:
var Ascore = 0;
var Bscore = 0;
var time = 10;
var ball = 0;
var rng = 0;
function timer() {
time--;
if (time >0) {
start();
setTimeout(timer, 1000);
console.log(time);
} else {
console.log('Game is over!');
console.log(teamAscore + ':' + teamBscore)
}
}
timer();
function start() {
rng = Math.random()*100;
if (rng < 50) {
aball();
console.log('Team A gets the ball!');
} else {
bball();
console.log('Team B gets the ball!');
}
}
function aball() {
rng = Math.random()*100;
if (rng > 50) {
Ascore + 2;
console.log('Team A scored 2 points!');
bball();
} else {
console.log('Team A missed!');
rng = Math.random()*100;
if (rng > 50) {
aball();
console.log('Team A rebounded!');
} else {
bball();
console.log('Team B got the rebound!');
}
}
}
function bball() {
rng = Math.random()*100;
if (rng > 50) {
Bscore + 2;
console.log('Team B scored 2 points!');
aball();
} else {
console.log('Team B missed!');
rng = Math.random()*100;
if (rng > 50) {
bball();
console.log('Team B rebounded!');
} else {
aball();
console.log('Team A got the rebound!');
}
}
}
我粘贴了代码,因为人们无法理解它。现在一切都有效但是它无限地一遍又一遍。我想在每个bball,aball功能之间延迟5秒。
答案 0 :(得分:1)
要在每次执行之间设置超时的循环中设置函数,请使用setInterval
:
var delayInMilliseconds = 5000;
setInterval(function () {
/* ... */
}, delayInMilliseconds);
要延迟函数执行(函数将执行一次),请使用setTimeout
:
var delayInMilliseconds = 5000;
setTimeout(function () {
/* ... */
}, delayInMilliseconds);
setInterval
和setTimeout
都可以在浏览器和node.js
进一步阅读:
requestAnimationFrame
回调process.nextTick
回调setTimeout
node.js参考setTimeout
浏览器参考setInterval
node.js参考setInterval
浏览器参考答案 1 :(得分:0)
你可以使用setInterval,clearInterval函数,让A在A完成播放时播放,所以:
* {
padding: 0px;
margin: 0px;
}
.menu-container{
background-color: gray;
margin: 30px;
position
}
.logo {
height: 10em;
display: inline-block;
padding: 1em;
transition: all 0.6s ease;
-webkit-transition: all 0.6s ease;
}
.menu {
float: right;
margin: 2em; 2em; 0; 0;
display: inline-block;
vertical-align: center;
}
答案 2 :(得分:0)
如果你的意思是想要根据rng调用任一函数,你可以这样做。请注意,我添加了var
以使rng
成为函数的本地。
function aball() {
var rng = Math.random()*100;
if (rng > 50) {
Ascore + 2;
console.log('Team A scored 2 points!');
bball();
} else {
console.log('Team A missed!');
rng = Math.random()*100;
if (rng > 50) {
setTimeout(aball, 5000);
console.log('Team A rebounded!');
} else {
setTimeout(bball, 5000);
console.log('Team B got the rebound!');
}
}
}
答案 3 :(得分:0)
我正在尝试重构您的代码以执行您需要它执行的操作。你能帮我澄清一些事情吗?我猜:aball和bball应该真正命名:
lakersBall()
pistonsBall()
你想为rng代表什么?这段代码没有完成,一旦我理解得更好,我会用你想要的结果清理它。
let pistonsScore = 0;
let lakersScore = 0;
let time = 10;
let ball = 0;
let rng = 0;
function timer() {
time--;
time > 0 ? console.log(time) : console.log(`Game is over! The pistons scored ${pistonsScore} and the Lakers scored: ${lakersScore}`);
}
function start() {
rng = Math.random() * 100;
rng < 50 ? console.log("Pistons got the ball!") : console.log("Lakers got the ball!")
}
function pistonsBall() {
rng = Math.random() * 100;
if (rng > 50) {
pistonsScore + 2;
console.log("Pistons scored 2 points!");
} else {
console.log("Pistons missed!");
rng = Math.random() * 100;
if (rng > 50) {
console.log("Pistons rebounded!");
} else {
console.log("Lakers got the rebound!");
}
}
}
function lakersBall() {
rng = Math.random() * 100;
if (rng > 50) {
lakersScore + 2;
console.log("Lakers scored 2 points!");
console.log("Lakers rebounded!");
}
if(rng < 50) {
console.log("Lakers missed!");
console.log("Pistons got the rebound!");
rng = Math.random() * 100;
}
}
答案 4 :(得分:-1)
尝试使用setTimeout(5000, function)
。请查看this question以查看setTimeout
的一些可能情况。