Javascript 如何在不刷新窗口的情况下重新启动计时器并刷新生命统计数据?
我正在用香草javascript尝试这个。我有一个虚拟宠物,它每秒钟都会随机失去生命统计信息。当这些值为零时,屏幕会改变。我的重启按钮将我带回我想要的屏幕,但我的计时器在点击重启后没有运行,因此统计数据不会下降 我觉得我的问题在于我的timePassed()和restart()函数(在代码的底部),但我现在陷入了困境。另外,我也不想让重启按钮刷新窗口,因为它是托管在codepen中的,并且它是不允许的 我真的卡住了。你能帮忙吗Javascript 如何在不刷新窗口的情况下重新启动计时器并刷新生命统计数据?,javascript,timer,settimeout,Javascript,Timer,Settimeout,我正在用香草javascript尝试这个。我有一个虚拟宠物,它每秒钟都会随机失去生命统计信息。当这些值为零时,屏幕会改变。我的重启按钮将我带回我想要的屏幕,但我的计时器在点击重启后没有运行,因此统计数据不会下降 我觉得我的问题在于我的timePassed()和restart()函数(在代码的底部),但我现在陷入了困境。另外,我也不想让重启按钮刷新窗口,因为它是托管在codepen中的,并且它是不允许的 我真的卡住了。你能帮忙吗 //set up levels to a start at a ma
//set up levels to a start at a maximum of 4
var hunger=4;
var happiness=4;
var health=4;
//create a random number 1-3
function answer(){
return Math.floor(Math.random() * 3)+1;
}
//time count starts at 0
var i=0;
//every minute take away 1 randomly from x, y or z
function decrease(){
if (answer() === 1){
hunger--;
}else if (answer()===2){
health--;
}else if(answer()===3){
happiness--;
}};
//show gameplay board
function showX(){
var x = document.getElementById("game");
x.style.display = "block";
}
function hideScreen() {
var y = document.getElementById("game");
y.style.display = "none";
}
function hideX() {
var z = document.getElementById("dead");
z.style.display = "none";
}
function changeStar() {
var star = document.getElementById('star');
if (happiness===4) {
star.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725934/virtual%20pet/s1.png";
}
else if (happiness===3){
star.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725934/virtual%20pet/s2.png";
}else if (happiness===2){
star.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725934/virtual%20pet/s3.png";
}else if (happiness===1){
star.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725934/virtual%20pet/s4.png";
}
}
function changeDonut() {
var donut = document.getElementById('donut');
if (hunger===4) {
donut.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725898/virtual%20pet/h1.png";
}
else if (hunger===3){
donut.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725898/virtual%20pet/h2.png";
}else if (hunger===2){
donut.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725898/virtual%20pet/h3.png";
}else if (hunger===1){
donut.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725898/virtual%20pet/h4.png";
}
}
function changeHeart() {
var heart = document.getElementById('heart');
if (health===4) {
heart.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725918/virtual%20pet/l1.png";
} else if (health===3){
heart.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725919/virtual%20pet/l2.png";
}else if (health===2){
heart.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725919/virtual%20pet/l3.png";
}else if (health===1){
heart.src = "https://res.cloudinary.com/dytmcam8b/image/upload/v1561725919/virtual%20pet/l4.png";
}
}
function x(){
document.getElementById('dead').innerHTML = '<span class="deadline">Oh, no! You killed Benny! You survived for ' + i + ' seconds. Can you do better next time?</span>';
}
//when clicking on pill, food , game or drink, make a sou
//on dying, make a sound.
//have a restart button on the death screen
document.getElementById("food").onclick= function feed(){
if (hunger<4){
hunger++;
}
}
document.getElementById("drink").onclick= function drink(){
if (hunger<4){
hunger++;
}
}
document.getElementById("pill1").onclick= function heal(){
if (health<4){
health++;
}
}
document.getElementById("games").onclick= function play(){
if (happiness<4){
happiness++;
}
}
var munchAudio = new Audio('https://res.cloudinary.com/dytmcam8b/video/upload/v1562342736/sounds/zapsplat_human_eat_biscuit_mouth_closed_28532.mp3');
var slurpAudio = new Audio('https://res.cloudinary.com/dytmcam8b/video/upload/v1562342736/sounds/zapsplat_human_drink_from_glass_slurp_single_21665.mp3');
var laughAudio = new Audio('https://res.cloudinary.com/dytmcam8b/video/upload/v1562343433/sounds/zapsplat_human_male_middle_aged_laugh_slightly_sinister_003_32379.mp3');
var pillAudio = new Audio('https://res.cloudinary.com/dytmcam8b/video/upload/v1562343433/sounds/noisecreations_SFX-NCFREE02_Synth-Swell.mp3');
function myAudioFunction(verb) {
if(verb === 'munch') {
munchAudio.play();
} else if(verb === 'slurp') {
slurpAudio.play();
} else if(verb === 'laugh'){
laughAudio.play();
}else if(verb === 'pill'){
pillAudio.play();
}
}
//function that uses random number to decrease vital stats and change images as the stats go down
function timePassed(){
i++;
answer();
decrease();
changeStar();
changeDonut();
changeHeart();
// once stats hit 0, stop timer and hide Benny to show death message
if (hunger==0|| happiness==0|| health==0){
clearInterval(timer);
x();
hideScreen();
}
}
var timer= setInterval('timePassed()', 1000);
//restart function
function restart(){
health=4;
happiness=4;
hunger=4;
showX();
hideX();
timePassed();
}
var button = document.getElementById("restart");
button.onclick = function() {
restart();
};
//将级别设置为最多4级
var=4;
幸福指数=4;
var-health=4;
//创建一个随机数1-3
函数答案(){
返回Math.floor(Math.random()*3)+1;
}
//时间计数从0开始
var i=0;
//每分钟从x、y或z中随机抽取1
函数减少(){
如果(答案()==1){
饥饿--;
}else if(answer()==2){
健康--;
}否则如果(答案()==3){
幸福--;
}};
//显示游戏性板
函数showX(){
var x=document.getElementById(“游戏”);
x、 style.display=“block”;
}
函数hideScreen(){
var y=document.getElementById(“游戏”);
y、 style.display=“无”;
}
函数hideX(){
var z=document.getElementById(“死”);
z、 style.display=“无”;
}
函数changeStar(){
var star=document.getElementById('star');
如果(幸福=4){
star.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725934/virtual%20pet/s1.png";
}
否则如果(幸福===3){
star.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725934/virtual%20pet/s2.png";
}否则如果(幸福===2){
star.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725934/virtual%20pet/s3.png";
}否则如果(幸福===1){
star.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725934/virtual%20pet/s4.png";
}
}
函数changeDonut(){
var donut=document.getElementById('donut');
如果(饥饿===4){
donut.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725898/virtual%20pet/h1.png";
}
否则如果(饥饿===3){
donut.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725898/virtual%20pet/h2.png";
}else if(饥饿===2){
donut.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725898/virtual%20pet/h3.png";
}else if(饥饿===1){
donut.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725898/virtual%20pet/h4.png";
}
}
功能改变心脏(){
var heart=document.getElementById('heart');
如果(运行状况===4){
heart.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725918/virtual%20pet/l1.png";
}否则如果(运行状况===3){
heart.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725919/virtual%20pet/l2.png";
}否则如果(运行状况===2){
heart.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725919/virtual%20pet/l3.png";
}否则如果(运行状况===1){
heart.src=”https://res.cloudinary.com/dytmcam8b/image/upload/v1561725919/virtual%20pet/l4.png";
}
}
函数x(){
document.getElementById('dead').innerHTML='哦,不!你杀了Benny!你活了'+i+'秒。下次你能做得更好吗?';
}
//当点击药丸、食物、游戏或饮料时,制作一个苏
//临死时,发出声音。
//在死亡屏幕上有一个重启按钮
document.getElementById(“food”).onclick=function feed(){
如果(饥饿请再次调用计时器变量,尝试结束重置功能,好吗
我怀疑调用restart函数时setInterval会被中断,因此再次调用time passes函数。你能简化代码并创建一个运行它的代码段吗?恐怕我不知道如果不去掉它有什么功能,我会怎么做。我想问题出在上个世纪的某个时候示例中的2个函数-timePassed()和restart()-可能是它的作用域,但我遇到了困难。这很有效。谢谢-我不知道为什么我自己没有细枝。