Javascript 在函数之间添加计时的最佳方法?
好的,我一直在做JS练习,我决定在控制台中做一个篮球模拟。对我来说,主要问题是在功能之间进行计时:Javascript 在函数之间添加计时的最佳方法?,javascript,node.js,google-chrome,console,Javascript,Node.js,Google Chrome,Console,好的,我一直在做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 {
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秒的延迟。尝试使用setTimeout5000,函数。检查以查看setTimeout的一些可能情况。要在循环中设置每次执行之间超时的函数,请使用setInterval:
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!');
}
}
}
要延迟函数执行功能将执行一次,请使用setTimeout:
setInterval和setTimeout都将在浏览器和node.js中工作
进一步阅读:
如果您正在处理游戏,请考虑阅读有关浏览器特定的回调。
有关node.js上的类似行为,但依赖于处理器标记,请阅读关于回调的内容
node.js参考
浏览器参考
node.js参考
浏览器参考
如果您的意思是希望基于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!');
}
}
}
我正在尝试重构您的代码,以完成您需要它完成的任务。你能帮我澄清一些事情吗?我猜:aball和bball应该真正命名为: 湖人队 皮斯顿球 你想为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;
}
}
你能再检查一下我的密码吗?我不明白我应该把这些计时放在什么地方才能让它正常工作。@PetrasVilkelis删除函数计时器并将bball和aball传递给setInterval,比如:setIntervalbball,5000;和setIntervalaball,5000;好的,但即使如此,它还是每隔一秒钟左右更新一次。每秒钟我就有5根木头。它好像在每次更新中都会加速并倍增。每次更新都会有越来越多的控制台日志。我希望它每5秒钟更新一次日志。这是由您的代码性质造成的-它的递归调用本身会从函数体中删除aball和bball。仅在setInterval中使用start,如:setIntervalstart,5000;我理解你的问题。请注意,我不会在setTimeout中的函数名之后使用,因为这将立即计算/执行console.log。另一个问题是,你每秒钟调用一次计时器,它调用开始,它调用任何一个函数,它们也不断地互相调用,所以你就像每秒调用四次函数一样,导致浏览器崩溃。不,你不明白,我希望它自我更新,像真正的游戏一样玩,而不是自己调用函数。自动地,用定时器。
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;
}
}