Я работаю над проектом, в котором мне нужна кнопка для разных действий в зависимости от того, когда она нажата. Я разработал функцию, установил переменную, соответствующую количеству нажатий кнопки, а затем ожидал, что код будет работать правильно. К сожалению, когда я его выполнил, я обнаружил, что код выполняется сразу, потому что после обновления кликов он запускается непрерывно. Вот что я пытался сделать:
function doSomething() {
if (clicks == 1){
//do something
}
if (clicks == 2) {
// do something
}
}
Вот полная программа:
function playGame(){
var games;
if (games < 16){
games += 1;
document.getElementById("weektracker").innerHTML = "Week " + games;
if (teamoverall > 74 && teamoverall < 81){
var x = Math.random();
if (x > 0.8){
randomWin();
}
if (x < 0.8){
randomLoss();
}
}
if (teamoverall > 80 && teamoverall < 86){
var x = Math.random();
if (x > 0.7){
randomWin();
}
if (x < 0.7){
randomLoss();
}
}
if (teamoverall > 85 && teamoverall < 88){
var x = Math.random();
if (x > 0.6){
randomWin();
}
if (x < 0.6){
randomLoss();
}
}
if (teamoverall > 87 && teamoverall < 90){
var x = Math.random();
if (x > 0.5){
randomWin();
}
if (x < 0.5){
randomLoss();
}
}
if (teamoverall > 89 && teamoverall < 93){
var x = Math.random();
if (x > 0.4){
randomWin();
}
if (x < 0.4){
randomLoss();
}
}
if (teamoverall > 92 && teamoverall < 96){
var x = Math.random();
if (x > 0.3){
randomWin();
}
if (x < 0.3){
randomLoss();
}
}
if (teamoverall > 95){
var x = Math.random();
if (x > 0.2){
randomWin();
}
if (x < 0.2){
randomLoss();
}
}
}
if (games == 16){
alert("Week 17" + "\n" + "Bye Week");
games += 1;
}
if (games == 17){
//Decide Playoff Outcome
if (wins < 6){
alert("You did not qualify for the NFL Playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("start").style.display = "block";
document.getElementById("record").style.display = "none";
}
if (wins > 5 && wins < 9){
var a1 = Math.random();
if (a1 > 0.3){
alert("You did not qualify for the NFL Playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("start").style.display = "block";
document.getElementById("record").style.display = "none";
}
if (a1 < 0.3){
games +=1;
alert("You have qualified for the Wild Card Round.");
document.getElementById("weektracker").innerHTML = "Wild Card Round";
var b1 = randomNumber(1,30);
teams.splice(b1,1);
document.getElementById("wildcardteam").innerHTML = teams[b1];
}
}
if (wins > 8 && wins < 11){
var a2 = Math.random();
if (a2 > 0.7){
alert("You did not qualify for the NFL Playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("record").style.display = "none";
document.getElementById("start").style.display = "block";
}
if (a2 < 0.7){
games +=1;
alert("You have qualified for the Wild Card Round.");
document.getElementById("weektracker").innerHTML = "Wild Card Round";
var b1 = randomNumber(1,30);
teams.splice(b1,1);
document.getElementById("wildcardteam").innerHTML = teams[b1];
}
}
if (wins > 10){
games +=1;
alert("You have qualified for the NFL Playoffs and have a bye in the Wild Card Round");
document.getElementById("weektracker").innerHTML = "Wild Card Round";
var b1 = randomNumber(1,30);
teams.splice(b1,1);
document.getElementById("divisionteam").innerHTML = teams[b1];
document.getElementById("wildcardteam").innerHTML = "Bye";
bye = 1;
}
}
if (games == 18){
if (bye == 0){
var a2 = Math.random();
if (teamoverall < 85){
if (a2 < 0.4){
randomWin();
games += 1;
document.getElementById("weektracker").innerHTML = "Division Round";
var b2 = randomNumber(1,29);
document.getElementById("divisionteam").innerHTML = teams[b2];
}
if (a2 > 0.4){
randomLoss();
alert("The " + teamname + " have been eliminated from the playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("record").style.display = "none";
document.getElementById("start").style.display = "block";
}
}
if (teamoverall > 84 && teamoverall < 90){
if (a2 < 0.5){
randomWin();
games += 1;
document.getElementById("weektracker").innerHTML = "Division Round";
var b2 = randomNumber(1,29);
document.getElementById("divisionteam").innerHTML = teams[b2];
}
if (a2 > 0.5){
randomLoss();
alert("The " + teamname + " have been eliminated from the playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("record").style.display = "none";
document.getElementById("start").style.display = "block";
}
}
if (teamoverall > 89 && teamoverall < 93){
if (a2 < 0.6){
randomWin();
games += 1;
document.getElementById("weektracker").innerHTML = "Division Round";
var b2 = randomNumber(1,29);
document.getElementById("divisionteam").innerHTML = teams[b2];
}
if (a2 > 0.6){
randomLoss();
alert("The " + teamname + " have been eliminated from the playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("record").style.display = "none";
document.getElementById("start").style.display = "block";
}
}
if (teamoverall > 92){
if (a2 < 0.75){
randomWin();
games += 1;
document.getElementById("weektracker").innerHTML = "Division Round";
var b2 = randomNumber(1,29);
document.getElementById("divisionteam").innerHTML = teams[b2];
}
if (a2 > 0.75){
randomLoss();
alert("The " + teamname + " have been eliminated from the playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("record").style.display = "none";
document.getElementById("start").style.display = "block";
}
}
}
if (bye == 1){
alert("Bye");
games += 1;
document.getElementById("weektracker").innerHTML = "Division Round";
var b2 = randomNumber(1,29);
document.getElementById("divisionteam").innerHTML = teams[b2];
}
}
if (games == 19){
alert("yo");
var a3 = Math.random();
if (teamoverall < 85){
if (a3 < 0.3){
randomWin();
games += 1;
document.getElementById("weektracker").innerHTML = "Division Round";
var b2 = randomNumber(1,29);
document.getElementById("divisionteam").innerHTML = teams[b2];
teams.splice(b2,1);
}
if (a3 > 0.3){
randomLoss();
alert("The " + teamname + " have been eliminated from the playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("record").style.display = "none";
document.getElementById("start").style.display = "block";
}
}
if (teamoverall > 84 && teamoverall < 90){
if (a3 < 0.45){
randomWin();
games += 1;
document.getElementById("weektracker").innerHTML = "Division Round";
var b2 = randomNumber(1,29);
document.getElementById("divisionteam").innerHTML = teams[b2];
teams.splice(b2,1);
}
if (a3 > 0.45){
randomLoss();
alert("The " + teamname + " have been eliminated from the playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("record").style.display = "none";
document.getElementById("start").style.display = "block";
}
}
if (teamoverall > 89 && teamoverall < 93){
if (a3 < 0.55){
randomWin();
games += 1;
document.getElementById("weektracker").innerHTML = "Division Round";
var b2 = randomNumber(1,29);
document.getElementById("divisionteam").innerHTML = teams[b2];
teams.splice(b2,1);
}
if (a3 > 0.55){
randomLoss();
alert("The " + teamname + " have been eliminated from the playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("record").style.display = "none";
document.getElementById("start").style.display = "block";
}
}
if (teamoverall > 92){
if (a3 < 0.7){
randomWin();
games += 1;
document.getElementById("weektracker").innerHTML = "Division Round";
var b2 = randomNumber(1,29);
document.getElementById("divisionteam").innerHTML = teams[b2];
teams.splice(b2,1);
}
if (a3 > 0.7){
randomLoss();
alert("The " + teamname + " have been eliminated from the playoffs.");
document.getElementById("weektracker").innerHTML = "Offseason";
document.getElementById("schedule").style.display = "none";
document.getElementById("play").style.display = "none";
document.getElementById("record").style.display = "none";
document.getElementById("start").style.display = "block";
}
}
}
Опять же, код просто выполняется без остановки, начиная с «if games == 17.»
Что я могу сделать, чтобы это исправить?