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> 

  • "It is necessary that when you click on - in the line Speed" - it may be unclear what the -. To do this, you must run the code, there will be a button in the settings -. - Alexander Lyamin

1 answer 1

Remove var from line

 var timer1 = setInterval(setgsOfSpeed, ... 

to use the variable timer1 , declared above.

The frequency of the running interval can not be changed. The timer must be cleared and restarted with the new value:

 speedMinus.onclick = function () { // Π£Π²Π΅Π»ΠΈΡ‡ΠΈΡ‚ΡŒ ΡΠΊΠΎΡ€ΠΎΡΡ‚ΡŒ ш changeSpeed = changeSpeed - 300; clearInterval(timer1); timer1 = setInterval(setgsOfSpeed, changeSpeed); }; 

Another option is to use the setTimeout call chain.

  • What I am inattentive .. Such nonsense ... Thank you very much for your help! )) - Alexander Lyamin
  • "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" - Can you help with this? - Alexander Lyamin
  • Thanks, it helped, now in the settings with the increase / decrease immediately clearly visible, what will be the speed. But I have another problem now. When I select the desired speed, I click on back, then on start. And then either the ball begins to move at the desired speed and everything is in order, or, for some reason, the settings open again, although I clicked start. Do not know what the problem may be now? - Alexander Lyamin
  • @AlexanderLyamin It's time to ask another question :). - Igor
  • Well, thank you very much for your help!) - Alexander Lyamin