Hello! Trying to create a game (clicker). Clicking on the settings, the function is launched, in which several more functions are nested. One of them is setgsOfSpeed. The function starts at a specified interval (the interval is a global variable changeSpeed). In this function, at each interval, the variable con is added 1. And then, the position of the block changes depending on the remainder of dividing con by 2 (that is, the block is moved n pixels to the left and then to the right). At this stage, everything works. It is necessary that when you click on - in the Speed ββline, 300 is subtracted from the changeSpeed ββvariable so that the function call interval is more frequent. For some reason, this does not work, but this is not the whole problem. If you forget about my question mentioned below and just go to the settings, and then click on the Back button, then the clearInterval action is added to this event, which should terminate the setgsOfSpeed ββfunction. But this does not happen for reasons I do not understand. When you click on start, the position function is launched, which starts at an interval equal to changeSpeed. But something incomprehensible happens: first, the menu is not removed, although I indicated that when I clicked on start, all elements take the value "none" in the display; secondly, my circle block changes the location too quickly, although changeSpeed ββ= 840 (if we take into account that we did not change the Speed ββvalue in the settings), that is, it seems that the block changes its position under the influence of two intervals, although I added the clearInterval event for the first interval. What could be the problem? I hope that explained more or less clear. Thanks in advance!
window.onload = function () { var start = document.getElementById("start"), // ΠΠ΅Π½Ρ setgs = document.getElementById("setgs"), back = document.getElementById("back"), exit = document.getElementById("exit"); var divSizePlus = document.getElementById("divSizePlus"), // ΠΠ°ΡΡΡΠΎΠΉΠΊΠΈ divSizeMinus = document.getElementById("divSizeMinus"), divSpeedPlus = document.getElementById("divSpeedPlus"), divSpeedMinus = document.getElementById("divSpeedMinus"); var sizePlus = document.getElementById("sizePlus"), // ΠΠ½ΠΎΠΏΠΊΠΈ Π² Π½Π°ΡΡΡΠΎΠΉΠΊΠ°Ρ
sizeMinus = document.getElementById("sizeMinus"), speedPlus = document.getElementById("speedPlus"), speedMinus = document.getElementById("speedMinus"); var timer; // Π’Π°ΠΉΠΌΠ΅Ρ Π΄Π»Ρ ΠΈΠ½ΡΠ΅ΡΠ²Π°Π»Π° Π² Π½Π°ΡΡΡΠΎΠΉΠΊΠ°Ρ
var timer1; // Π’Π°ΠΉΠΌΠ΅Ρ Π΄Π»Ρ ΠΈΠ½ΡΠ΅ΡΠ²Π°Π»Π° ΠΏΡΠΈ Π½Π°ΠΆΠ°ΡΠΈΠΈ Π½Π° ΡΡΠ°ΡΡ var circle = document.getElementById("circle"); // Π‘Π°ΠΌ Π±Π»ΠΎΠΊ (Π² Π²ΠΈΠ΄Π΅ ΡΠ°ΡΠ°) var score = document.getElementById("score"), // ΠΡΠΊΠΈ points = document.getElementById("points"); var size = document.getElementById("size"); // ΠΠ»Ρ ΠΏΠΎΠΊΠ°Π·Π° ΡΠ°Π·ΠΌΠ΅ΡΠ° ΡΠ°ΡΠ° var con = 0; // ΠΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ Π² ΡΡΠ½ΠΊΡΠΈΠΈ Π² Π½Π°ΡΡΡΠΎΠΉΠΊΠ°Ρ
Π΄Π»Ρ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΡ ΡΠ°ΡΠ° var changeSpeed = 840; // ΠΠ½Π°ΡΠ΅Π½ΠΈΠ΅, ΡΠΊΠ°Π·ΡΠ²Π°ΡΡΠΈΠ΅ Ρ ΠΊΠ°ΠΊΠΈΠΌ ΠΈΠ½ΡΠ΅ΡΠ²Π°Π»ΠΎΠΌ Π²ΡΠ·ΡΠ²Π°ΡΡ ΡΡΠ½ΠΊΡΠΈΡ. setgs.onclick = function () { // ΠΠ°ΠΏΡΡΠΊ ΡΡΠ½ΠΊΡΠΈΠΈ ΠΏΡΠΈ Π½Π°ΠΆΠ°ΡΠΈΠΈ Π½Π° Settings back.style.margin = "90px 0 0 500px"; divSizePlus.style.display = "block"; divSizeMinus.style.display = "block"; divSpeedPlus.style.display = "block"; divSpeedMinus.style.display = "block"; back.style.display = "block"; circle.style.display = "block"; size.style.display = "block"; start.style.display = "none"; setgs.style.display = "none"; var plusSize = 80, minusSize = 80, defSize = 80, sumPlus = 0, sumMinus = 0, radius = 50, counter = 5; sizePlus.onclick = function () { // Π£Π²Π΅Π»ΠΈΡΠΈΡΡ ΡΠ°Π·ΠΌΠ΅Ρ if (defSize > 120) { plusSize.style.cursor = "none"; } plusSize = defSize; sumPlus = plusSize + counter; circle.style.borderRadius = radius + "px"; circle.style.width = sumPlus + "px"; circle.style.height = sumPlus + "px"; plusSize = sumPlus; defSize = plusSize; radius = radius + counter; size.innerHTML = "(" + defSize + ")"; }; sizeMinus.onclick = function () { // Π£ΠΌΠ΅Π½ΡΡΠΈΡΡ ΡΠ°Π·ΠΌΠ΅Ρ if (defSize < 20) { minusSize.style.cursor = "none"; } minusSize = defSize; sumMinus = minusSize - counter; circle.style.width = sumMinus + "px"; circle.style.height = sumMinus + "px"; minusSize = sumMinus; defSize = minusSize; size.innerHTML = "(" + defSize + ")"; }; speedMinus.onclick = function () { // Π£Π²Π΅Π»ΠΈΡΠΈΡΡ ΡΠΊΠΎΡΠΎΡΡΡ Ρ changeSpeed = changeSpeed - 300; }; function setgsOfSpeed () { // Π€ΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΡ ΡΠ°ΡΠ° Π² Π½Π°ΡΡΡΠΎΠΉΠΊΠ°Ρ
con++; if ( (con%2) === 1 ) { circle.style.left = -330 + "px"; } else if ( (con%2) === 0 ) { circle.style.left = -160 + "px"; } } var timer1 = setInterval(setgsOfSpeed, changeSpeed); // ΠΠ΅ΡΠ²ΡΠΉ ΠΈΠ½ΡΠ΅ΡΠ²Π°Π» } back.onclick = function () { // ΠΠ½ΠΎΠΏΠΊΠ° back clearInterval(timer1); // ΠΡΠΈΡΡΠΊΠ° ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΠΈΠ½ΡΠ΅ΡΠ²Π°Π»Π° divSizePlus.style.display = "none"; divSizeMinus.style.display = "none"; divSpeedPlus.style.display = "none"; divSpeedMinus.style.display = "none"; circle.style.display = "none"; back.style.display = "none"; size.style.display = "none"; start.style.display = "block"; setgs.style.display = "block"; } exit.onclick = function () { // ΠΠ½ΠΎΠΏΠΊΠ° Π²ΡΡ
ΠΎΠ΄Π° ΠΈΠ· ΠΈΠ³ΡΡ clearInterval(timer); // ΠΡΠΈΡΠΊΠ° Π²ΡΠΎΡΠΎΠ³ΠΎ ΡΠ°ΠΉΠΌΠ΅ΡΠ° (ΡΡΠΎ Π·Π½Π°ΡΠΈΡ, ΡΡΠΎ ΠΈΠ³ΡΠ° Π·Π°Π²Π΅ΡΡΠ΅Π½Π° ΠΈ Π·Π°ΠΏΡΡΠΊΠ°ΡΡ ΡΡΠ½ΠΊΡΠΈΡ Π±ΠΎΠ»ΡΡΠ΅ Π½Π΅ Π½ΡΠΆΠ½ΠΎ) circle.style.left = -150 + "px"; circle.style.top = 43 + "px"; circle.style.backgroundColor = "aqua"; circle.style.display = "none"; exit.style.display = "none"; points.style.display = "none"; score.style.display = "none"; start.style.display = "block"; setgs.style.display = "block"; } start.onclick = function () { // ΠΠ½ΠΎΠΏΠΊΠ° start Π΄Π»Ρ Π½Π°ΡΠ°Π»Π° ΠΈΠ³ΡΡ start.style.display = "none"; setgs.style.display = "none"; circle.style.display = "block"; exit.style.display = "block"; score.style.display = "block"; points.style.display = "block"; var count = 0; circle.onclick = function () { circle.style.backgroundColor = "hsl(" + getRandom(0,360)+",75%,50%)"; count++; score.innerHTML = count; } function position () { // Π€ΡΠ½ΠΊΡΠΈΡ Π΄Π»Ρ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΡ ΡΠ°ΡΠ° Π²ΠΎ Π²ΡΠ΅ΠΌΡ ΠΈΠ³ΡΡ var x = window.innerWidth, y = window.innerHeight; x = x - 390; y = y - 100; circle.style.left = getRandom(1, x - 200) + "px"; circle.style.top = getRandom(1, y - 200) + "px"; } timer = setInterval(position, 840); // ΠΡΠΎΡΠΎΠΉ ΠΈΠ½ΡΠ΅ΡΠ²Π°Π» function getRandom(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } } } div { position: absolute; margin: 230px 0 0 600px; cursor: pointer; } /* Π’Π΅ΠΊΡΡ Π² ΠΌΠ΅Π½Ρ */ span { position: absolute; left: 35px; top: 4px; cursor: default; } body { background-image: url(images/fon.jpg); font-family: Arial Black; color: bisque; font-size: 25px; } #circle { left: -160px; top: 53px; width: 80px; height: 80px; background-color: aqua; border-radius: 50px; display: none; cursor: default; } /* ΠΌΠ΅Π½Ρ */ #start { top: 10px; } #setgs { top: 30px; padding-top: 25px; } #back { top: 280px; left: 108px; display: none; } #exit { display: none; top: -218px; left: 696px; } #points { left: 8px; top: 10px; display: none; } #score { margin-top: 35px; display: none; } /* Π±Π»ΠΎΠΊΠΈ Ρ ΠΊΠ½ΠΎΠΏΠΊΠ°ΠΌΠΈ Π½Π°ΡΡΡΠΎΠ΅ΠΊ */ #divSizePlus { top: 50px; left: 70px; display: none; } #divSizeMinus { top: 50px; display: none; } #divSpeedPlus { left: 70px; top: 90px; display: none; } #divSpeedMinus { top: 90px; display: none; } /* ΠΊΠ½ΠΎΠΏΠΊΠΈ + - */ input { background-color: burlywood; border: 1px solid burlywood; width: 26px; height: 25px; cursor: pointer; } #size { position: absolute; top: 275px; left: 800px; font-size: 16px; display: none; } <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Clicker</title> <link href="Test.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="start">Start</div> <div id="setgs">Settings</div> <div id="back">Back</div> <div id="divSizePlus"><input type="button" value="+" id="sizePlus"><span>Size</span></div> <div id="divSizeMinus"><input type="button" value="-" id="sizeMinus"></div> <div id="divSpeedPlus"><input type="button" value="+" id="speedPlus"><span>Speed</span></div> <div id="divSpeedMinus"><input type="button" value="-" id="speedMinus"></div> <div id="circle"></div> <div id="exit">Exit</div> <span id="points">ΠΡΠΊΠΈ: </span> <p id="score"></p> <p id="size"></p> <script src="Test.js"></script> </body> </html>