Is the progress bar able to duplicate itself?

I'm currently facing an issue with my progress bar, or as I like to call it, a depletion bar. The purpose of this bar is simple - whenever it's clicked, the button resets itself. However, if the bar reaches 100%, the player loses. The strange problem I encountered was that instead of functioning properly, the bar started replicating itself and rendering multiple progress bars simultaneously.

Below is the code snippet:

function move() {
  var elem = document.getElementById("DPbar");
  var width = 0; /* Starting percentage */
  var id = setInterval(frame, 100); /* Speed of depletion bar */
  function frame() {
    if (width >= 13) { /* Length of depletion bar (ending percentage) */
      clearInterval(id);
      alert("You Lose, reload (Ctrl + R) the page to play again!"); /* Message displays after hitting 100% (or whatever is the final percentage) */
    } else {
      width++;
      elem.style.width = width + '%'; /* Speed of depletion bar also (idk?) */
      elem.innerHTML = width * 7.69230769230 + '%'; /* Multiplier of percentage */
    }
  }
}
html,
body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #9898ff;
  opacity: 0.8;
  background-image: linear-gradient(to right, #4349c2, #4349c2 25px, #5e5eff 25px, #5e5eff);
  background-size: 50px 100%;
}

.PringleButton {
  background-color: #FFEA00;
  /* Pringle Color */
  border: 5px solid black;
  color: black;
  /* Text Color */
  padding: 86px 52px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 32px;
  border-radius: 100%;
  cursor: copy;
}

.PringleButton:hover {
  background-color: rgba(236, 236, 57, 0.667)
}

.PringleButton:active {
  background-color: rgba(236, 236, 57, 0.667);
  transform: scale(0.9, 0.9);
}

.DPbar {
  position: absolute;
  bottom: 200px;
  left: 725px;
  border: 2px solid black;
  background-color: seagreen;
  border-radius: 25%;
  cursor: not-allowed;
}
<!--button and depletion bar functionality-->

<!--TO DO: 1: Make DP bar length shorter + center it (DONE)
                       2: DP bar total percentage = 100% (DONE...kinda...)
                       3: Tweak depletion speed (DONE)
                       4: DP bar does not push pringle button (DONE)
                       5: Clicking multiple times does not clone the DP bar-->

<div id="DPbar" class="DPbar" style="width:0%">0%</div>
<!-- Starting percentage of depletion bar-->
<br>
<button class="PringleButton" onclick="move()">Pringle!</button>

Answer №1

Adjusted the code layout for better understanding. It is crucial to clear the interval if it already exists when the button is clicked, as indicated in the comments

<!DOCTYPE html>
<!--Pringle Clicker 1.0-->
<!--The DP (Depletion Bar) code was originally sourced from w3schools.com, but has been extensively modified-->
<html>
<head>
    <title>Pringle Clicker v1.0</title>

    <style> html, body {
        height: 100%;
        margin: 0;
    }

    body {
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: #9898ff;
        opacity: 0.8;
        background-image: linear-gradient(to right, #4349c2, #4349c2 25px, #5e5eff 25px, #5e5eff );
        background-size: 50px 100%;
    }

    .PringleButton {
        background-color: #FFEA00; /* Pringle Color */
        border: 5px solid black;
        color: black; /* Text Color */
        padding: 86px 52px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 32px;
        border-radius: 100%;
        cursor: copy;
    }

    .PringleButton:hover {background-color: rgba(236, 236, 57, 0.667)}

    .PringleButton:active {
        background-color: rgba(236, 236, 57, 0.667);
        transform: scale(0.9, 0.9);
    }

    .DPbar {
        position: absolute;
        bottom: 200px;
        left: 725px;
        border: 2px solid black;
        background-color: seagreen;
        border-radius: 25%;
        cursor: not-allowed;
    }

    </style>
</head>
<body>
<!--button and depletion bar functionality-->

<!--TO DO: 1: Make DP bar length shorter + center it (DONE)
           2: DP bar total percentage = 100% (DONE...kinda...)
           3: Tweak depletion speed (DONE)
           4: DP bar does not push pringle button (DONE)
           5: Clicking multiple times does not clone the DP bar-->

<div id="DPbar" class="DPbar" style="width:0%">0%</div> <!-- Starting percentage of depletion bar-->
<br>
<button class="PringleButton" onclick="move()">Pringle!</button>


<script>
    var id = undefined; // --> RELOCATED ID OUTSIDE
    function move() {
        var elem = document.getElementById("DPbar");
        var width = 0; /* Starting percentage */
        if (id) { // --> CLEAR ID IF EXISTS
            clearInterval(id);
        }
        function frame() {
            if (width >= 13) { /* Length of depletion bar (ending percentage) */
                clearInterval(id);
                alert("You Lose, reload (Ctrl + R) the page to play again!"); /* Message displays after hitting 100% (or whatever is the final percentage) */
            } else {
                width++;
                elem.style.width = width + '%'; /* Speed of depletion bar also (idk?) */
                elem.innerHTML = width * 7.69230769230 + '%'; /* Multiplier of percentage */
            }
        }
        id = setInterval(frame, 100); /* Speed of depletion bar */ 
    }
</script>



</body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the reason behind getComputedStyle having visibility set to visible?

What is the reason behind getComputedStyle always returning element visibility as visible even when no explicit visibility has been set? For instance: getComputedStyle($('#block1')[0],null).visibility; --- "visible" Meanwhile: $('#block1&a ...

Removing data entry from MySQL database table using PDO

I'm facing an issue with deleting a row from my database. Despite trying various methods like PDO and MySQL, the data is not getting deleted, but it shows as if it has been deleted. Can someone please help me identify what might be wrong with my code? ...

Notification window when the page is being loaded

My goal is to have a module or alert box display when my website is opened, and then disappear after 5 minutes. To see an example of what I'm looking for, take a look at this website: On that website, there is a facebook.com box that automatically d ...

What steps can I take to prevent Javascript/JQuery from repositioning other elements on the webpage?

Is there a way for the animation code to only affect the show/hide toggle buttons and not interfere with the other jQuery code I have implemented for hiding and showing div text? When I included the animate code in my js page, it caused issues with the exi ...

Having no capability to manipulate CSS using jquery

In one of my divs, the CSS is set to have display: none .cookie_policy { color: white; text-align: justify; width: 50%; margin: 2% 25%; line-height: 20px; display: block; font-family: Arial,Helvetica,sans-serif; font-style: italic; display:none; } <div ...

Restrict the number of dynamic form elements to a maximum of 10 entries

I am working on a feature where users can refer their friends and the data will be saved in a database. <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <script type='text/javascript' sr ...

combining: enhancing the value of the background-image property

Here is a mixin that I would like to modify: .a () {background-image: url(one.png);} I want to create a new class .b which inherits properties from .a, but also includes an additional background image layer, like this: .b { .a; ...

Is it possible to maintain HTML, JS, and CSS files as separate entities when developing Vue.js components, similar to how it is

Is it possible to maintain separate HTML, JS, and CSS files while creating Vue.js components? I recently read the "Why Vue.js doesn't support templateURL" article which discusses this topic. "Proper modularization is a necessity if you want to bu ...

The design breaks occur exclusively in the Android default browser

I am experiencing issues with the design of a nested div element when viewed in the default Android browser. Unfortunately, I don't have access to tools that would allow me to identify the cause of this problem. However, the design functions correctly ...

Designing an HTML and CSS footer navigation bar

Can you help me create a footer menu using HTML and CSS? Check out this image for reference Any tips on how to style it with cascading style sheets? <footer> <ul> <li><a href="">Home</a> </li> <li> ...

Issues with Internet Explorer causing headaches with CSS :first-child selector malfunctioning, experiencing strange behavior when utilizing Google Web Fonts

Having some trouble with the layout of a website, particularly in IE. The issue can be observed at . When comparing the page in IE to Firefox, there are noticeable differences. Firstly, the top module on the right side is not displaying correctly in IE. I ...

Floating above a divided rectangle div

Imagine a rectangular div divided into two parts, each part forming an L shape (as illustrated below), with region 1 representing the left-side L and region 2 representing the right-side L. I am interested in changing the background color of region 1 on h ...

Are there any pre-existing pop-up methods available for AngularJS?

Is there a way to create a simple pop-up window with static text and just one "Ok" button? In Java/Swing, it's possible to achieve this with just one line of code using pre-defined classes. However, when it comes to Angular pop-ups, it seems like the ...

When a user clicks on an anchor tag, the corresponding value of the checked item is then returned

I have 3 sets of radio buttons. When a specific anchor with the "round" class is clicked, two actions should occur: The associated set of radio buttons needs to become visible The value of the checked input for that element should be returned. I am ...

Issue with live Chrome page: SVG not displaying

Despite trying different solutions, my SVG is still not appearing correctly on my live website. It displays perfectly on my local server and computer, but once the site is live, the SVG disappears. The HTML code for my SVG uses an img tag wrapped in an a t ...

Fluid Bootstrap Container Experiencing Alignment Issues with Rows when CSS Height Property set to 100%

My Bootstrap container has a fluid width and I'm trying to align three rows within a sidebar to sit at the top, middle, and bottom. I came across this demo in the Bootstrap documentation which shows how to do it for full-width layout, but I need it fo ...

javascript code producing incorrect HTML

I created a script to populate a selectbox with various options. Initially, the data is in the form of a string like this: "key=value;key2=value2;etc...": //splitting the string to separate different options for the selectbox var values = data.split(&apo ...

In the Opera browser, the closing script tags seem to mysteriously vanish

I recently built a website using bootstrap and implemented the JQuery load function to merge separate HTML files into one for better organization. The site is currently live on github pages. However, I've encountered an issue specifically with Opera ...

Show the res.send() method from express.js without replacing the existing html content

Currently, I am in the process of developing a calculator application using express.js. The app needs to handle get requests for an HTML file and post requests that capture user input and provide the response accurately. My challenge lies in showcasing the ...

How can I load only specific images on a webpage using HTML?

I attempted to implement an image filter for my website by using the code below: <script> function myFunction() { // Initialize variables var input, filter, ul, li, a, i; input = document.getElementById('myInput'); filter = input.value.toU ...