Search code examples
javascripthtmlpage-refresh

How to stop page from auto refreshing with JavaScript?


I am quite new to web dev and currently, I work on a JS course. I have a problem with the automatic page refreshing that happens in Chrome and Safari also.

I have read that this refreshing can be caused by input submission in HTML but this problem occurs even when I do not use any type of button. In the code below, I tried to make a counter of the best score of all games when I guess the numbers, but I cannot record this score because my browser randomly refreshes my localhost and therefore all data is lost. As I said this problem occurs even when I do not use any type of submitting in my code (page randomly refreshes itself).

I have tried to disable adblocker in my browsers, but this did not help. Also, I tried to use window.stop() at the start and end of the code, but this did not seem desirable because safari always showed that it is still loading the page. Also, I have read that this can be caused by some sort of RAM optimization in the browser and therefore I tried to execute the code with only one tab open but this did not help either. I use Live Server extension with VS Code and therefore I do not run live server with node.js through terminal.

Because this problem occurs in multiple browsers I figured that it can be solved by some additional code in JS for example. Do you have any idea how could I solve this problem? I have run out of ideas at this point.

My Code:

"use strict";

let secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highScore = 0;

document.querySelector(".check").addEventListener("click", function() {
  const guess = Number(document.querySelector(".guess").value);

  if (!guess) {
    // When there is no input
    document.querySelector(".message").textContent = "No Number";
  } else if (secretNumber === guess) {
    // When correct guess
    document.querySelector(".number").textContent = secretNumber;
    document.querySelector(".message").textContent = "Correct guess! 🎉";
    document.querySelector("body").style.backgroundColor = "#228B22";

    if (score > highScore) {
      highScore = score;
      document.querySelector(".highscore").textContent = highScore;
    }
  } else if (secretNumber < guess) {
    // When guess is too high
    if (score > 1) {
      score--;
      document.querySelector(".score").textContent = score;
      document.querySelector(".message").textContent = "Too High";
    } else {
      document.querySelector(".score").textContent = 0;
      document.querySelector(".message").textContent = "You lost the game.";
    }
  } else {
    // When guess is too low
    if (score > 1) {
      score--;
      document.querySelector(".score").textContent = score;
      document.querySelector(".message").textContent = "Too Low";
    } else {
      document.querySelector(".score").textContent = 0;
      document.querySelector(".message").textContent = "You lost the game.";
    }
  }

  // Reseting Game With Button
  document.querySelector(".again").addEventListener("click", function() {
    score = 20;
    document.querySelector(".score").textContent = score;
    secretNumber = Math.trunc(Math.random() * 20) + 1;
    document.querySelector(".message").textContent = "Start Guessing";
    document.querySelector(".number").textContent = "?";
    document.querySelector("body").style.backgroundColor = "#222222";
    document.querySelector(".guess").value = "";
  });
});
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="style.css" />
  <title>Guess My Number!</title>
</head>

<body>
  <header>
    <h1>Guess My Number!</h1>
    <p class="between">(Between 1 and 20)</p>
    <button class="btn again">Again!</button>
    <div class="number">?</div>
  </header>
  <main>
    <section class="left">
      <input type="number" class="guess" />
      <button class="btn check">Check!</button>
    </section>
    <section class="right">
      <p class="message">Start guessing...</p>
      <p class="label-score">💯 Score: <span class="score">20</span></p>
      <p class="label-highscore">
        🥇 Highscore: <span class="highscore">0</span>
      </p>
    </section>
  </main>
  <script src="script.js"></script>
</body>

</html>

Currently, I use this Chrome version "Version 92.0.4515.159 (Official Build) (arm64)" and this Safari version "Version 14.1.2 (16611.3.10.1.3)" with MacBook Air(2020) with M1 chip and 16 GB of RAM.


Solution

  • Apparently, there is a problem with refreshing browser windows with localhost when loading code from external SSD. I disabled the Live Server extension in VS Code and installed node.js and run the live server from there but this did not help either. When I moved my files from the external SSD into the machine everything worked correctly.