简体   繁体   English

请告诉我如何在JS中重复这个过程?

[英]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.

相关问题 有人可以告诉我如何阅读以下内容:/\\.(js)$/ - Can someone please tell me how to read the following: /\.(js)$/ 有人可以告诉我如何简化这段代码吗? - Could someone please tell me how I could simplify this code? 谁能告诉我这个 javaScript function 是如何工作的? - Can anyone tell me how this javaScript function works please? 请告诉我如何编写Chrome扩展程序代码以全屏显示 - please tell me how to write code for chrome extension to be in full screen 请告诉我如何以及在何处定义“ addEventListener” - Please tell me how and where we will define “addEventListener” 有人可以告诉我如何删除或隐藏此 JavaScript 代码吗? - Can someone please tell me how to remove or hide this JavaScript code? 有人可以告诉我为什么这个js函数无法在加载时运行吗? - Can someone please tell me why this js function doesn't run on load? 请告诉我此JavaScript代码有什么错误 - Please tell me what is error in this javascript code 请告诉我如何在javascript中访问gridview的单元格的值 - please tell me how can i access value of gridview's cell in javascript 使用 javascript,请告诉我如何获取结果数组,如下面的“resultList”。 类似于 SQL 中的 UPDATE 语句 - With javascript, please tell me how to get result array like below "resultList". It is similar to the UPDATE statement in SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM