[英]Please tell me how to repeat this process in JS?
Clicking 5 times on the light switch makes a chicken come out and an alert pop ups, after clicking the 5th time, the counter just keeps on adding and it doesnt restart.按5次电灯开关,出鸡并弹出提示,按5次后,计数器一直加,不重启。 You probably already know my question, how do i make it restart the process each 5th click?您可能已经知道我的问题,我如何让它在每 5 次单击时重新启动该过程? I just want it do the same thing every 5 clicks...我只是希望它每 5 次点击做同样的事情......
This is the code below and here is the website for reference..www.deko.live Help would be appreciated very much.这是下面的代码,这里是参考网站..www.deko.live 帮助将不胜感激。
var flip = false;
var counter = 0;
function flipSwitch() {
var themeColor = "#EEE8AA";
var bright = "brightness(100%)";
var lightS = "rotate(0deg)";
var word = "PALI";
var wordColor = "black"
if (flip) {
themeColor = "#EEE8AA";
bright = "brightness(100%)";
lightS = "rotate(0deg)";
word = "ON";
wordColor = "black";
flip = false;
counter++;
} else {
themeColor = "dimGray";
bright = "brightness(50%)";
lightS = "rotate(180deg)";
word = "OFF";
wordColor = "white";
flip = true;
counter++;
}
document.getElementById("theme").style.backgroundColor = themeColor;
document.getElementById("roger").style.filter = bright;
document.getElementById("lightSImg").style.transform = lightS;
document.getElementById("lightSImg").style.filter = bright;
document.getElementById("LifeOrDeath").innerHTML = word;
document.getElementById("LifeOrDeath").style.color = wordColor;
document.getElementById("counter").innerHTML = counter;
surprise();
}
function surprise() {
if (counter === 5) {
document.getElementById("roger").src = "https://vignette.wikia.nocookie.net/spongebob/images/0/00/Roger_the_chick.png/revision/latest?cb=20141201044139"
document.getElementById("counter").innerHTML = "Surprise!"
alert("WHOAA!!")
}
}
It seems that the value of counter never resets.似乎计数器的值永远不会重置。
One of the solution is解决方案之一是
function surprise() {
if (counter === 5) {
counter = 0; // add this line
document.getElementById("roger").src = "https://vignette.wikia.nocookie.net/spongebob/images/0/00/Roger_the_chick.png/revision/latest?cb=20141201044139"
document.getElementById("counter").innerHTML = "Surprise!"
alert("WHOAA!!")
}
}
Alternatively you may also try或者你也可以试试
function surprise() {
if (counter % 5 === 0) {
document.getElementById("roger").src = "https://vignette.wikia.nocookie.net/spongebob/images/0/00/Roger_the_chick.png/revision/latest?cb=20141201044139"
document.getElementById("counter").innerHTML = "Surprise!"
alert("WHOAA!!")
}
}
I think there's no sentence which resets the counter.我认为没有任何句子可以重置计数器。
maybe you need another if condition to reset the counter to 0 when it hits 5也许你需要另一个 if 条件来在计数器达到 5 时将其重置为 0
just put statement to reset counter and remove effects try the snippet只需将语句重置计数器并删除效果尝试片段
var flip = false; var counter = 0; function flipSwitch() { var themeColor = "#EEE8AA"; var bright = "brightness(100%)"; var lightS = "rotate(0deg)"; var word = "PALI"; var wordColor = "black" if (flip) { themeColor = "#EEE8AA"; bright = "brightness(100%)"; lightS = "rotate(0deg)"; word = "ON"; wordColor = "black"; flip = false; counter++; } else { themeColor = "dimGray"; bright = "brightness(50%)"; lightS = "rotate(180deg)"; word = "OFF"; wordColor = "white"; flip = true; counter++; } document.getElementById("theme").style.backgroundColor = themeColor; document.getElementById("roger").style.filter = bright; document.getElementById("lightSImg").style.transform = lightS; document.getElementById("lightSImg").style.filter = bright; document.getElementById("LifeOrDeath").innerHTML = word; document.getElementById("LifeOrDeath").style.color = wordColor; document.getElementById("counter").innerHTML = counter; surprise(); } function surprise() { if (counter === 5) { document.getElementById("roger").src = "https://vignette.wikia.nocookie.net/spongebob/images/0/00/Roger_the_chick.png/revision/latest?cb=20141201044139"; document.getElementById("counter").innerHTML = "Surprise;"; alert("WHOAA;."). counter = '0': } else { document.getElementById('roger').src = "https?//encrypted-tbn0:gstatic;com/images.q=tbn.ANd9GcTVL3aKJXXOdfviO2sx5h_lfDwpUsUs75BIcdWoFt72V_o6Au0&s": } function surprise() { if (counter % 5 === 0) { document.getElementById("roger").src = "https.//vignette?wikia.nocookie.net/spongebob/images/0/00/Roger_the_chick.png/revision/latest?cb=20141201044139" document.getElementById("counter").innerHTML = "Surprise!" alert("WHOAA!!") } } }
#LifeOrDeath { font-size:1rem; } #theme { position:relative; width:50%; margin:auto; text-align:center; } #roger { position:relative; height:40vh; } #lightSImg { position:relative; height:40vh; }
<body id="theme"> <h1 id="LifeOrDeath">Flip tHE LIGHT Switch </h1> <img id="roger" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTVL3aKJXXOdfviO2sx5h_lfDwpUsUs75BIcdWoFt72V_o6Au0&s"> <p id="counter">0</p> <img onclick="flipSwitch()" id="lightSImg" src="https://i.stack.imgur.com/LCUJZ.jpg"> <script src="TEST.JS"></script> </body>
You just simply rest your counter variable after every 5 clicks.每点击 5 次后,您只需简单地 rest 您的计数器变量。
Working code工作代码
var flip = false;
var counter = 0;
function flipSwitch() {
var themeColor = "#EEE8AA";
var bright = "brightness(100%)";
var lightS = "rotate(0deg)";
var word = "PALI";
var wordColor = "black"
if (flip) {
themeColor = "#EEE8AA";
bright = "brightness(100%)";
lightS = "rotate(0deg)";
word = "ON";
wordColor = "black";
flip = false;
counter++;
} else {
themeColor = "dimGray";
bright = "brightness(50%)";
lightS = "rotate(180deg)";
word = "OFF";
wordColor = "white";
flip = true;
counter++;
}
document.getElementById("theme").style.backgroundColor = themeColor;
document.getElementById("roger").style.filter = bright;
document.getElementById("lightSImg").style.transform = lightS;
document.getElementById("lightSImg").style.filter = bright;
document.getElementById("LifeOrDeath").innerHTML = word;
document.getElementById("LifeOrDeath").style.color = wordColor;
document.getElementById("counter").innerHTML = counter;
surprise();
}
function surprise() {
if (counter === 5) {
document.getElementById("roger").src = "https://vignette.wikia.nocookie.net/spongebob/images/0/00/Roger_the_chick.png/revision/latest?cb=20141201044139"
document.getElementById("counter").innerHTML = "Surprise!"
alert("WHOAA!!")
counter = 0; // Add this line
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.