Issue with JavaScript Variable Not Reflecting Changes in Progress Bar

I'm in the midst of developing a game and have incorporated a progress bar to facilitate the incrementation of certain values on my HTML page. However, once the progress bar hits 100%, it updates the cost and level only once. After that initial update, it fails to refresh the hard drive level regardless of how many times you trigger it. How can I ensure the variable keeps updating?

//statistics
var money = document.getElementById('cash');
var memory = document.getElementById('ram');
var connection = document.getElementById('internet');
var storage = document.getElementById('harddrive');
var levels = document.getElementById('level');
//virtual store
function levelup(t){
var bar = document.getElementById('lvelprogress');
bar.value = bar.value + t;
t = 0
if(t == 100){
bar.value = 100;
};
};
function upgradehd(al) { 
var hdc = document.getElementById('hdc');
var hdl = document.getElementById('hdl');
var hdLevel = 1;
var newStorage = 250;
var timeoutValue = levels * 20;
var newHdc = Math.floor(2.5 * hdc.innerHTML); 
var progressBar = document.getElementById('progressBar');
var statusInfo = document.getElementById('status');
statusInfo.innerHTML = al+"%";
progressBar.value = al;
document.getElementById('lvelprogress').max = 100;
al++
var simulation = setTimeout("upgradehd("+al+")",timeoutValue);
if(al == 100){
var experiencePoints = hdc.innerHTML / 4;
levelup(experiencePoints);
statusInfo.innerHTML = "100%";
hdl.innerHTML = hdLevel + 1;
hdc.innerHTML = newHdc;
storage.innerHTML = newStorage + 50;
al=0;
statusInfo.innerHTML = al+"%";
progressBar.value = 0;
clearTimeout(simulation);
};
};
.upgrade{
background-color: #66ff33;
width: 200px;
}
.shop{
background-color: black;
color: #66ff33;
width: 200px;
border: 5px solid #cccccc
}
.stats{
float: right;
height: 175px;
width: 150px;
background-color: #000;
color: #66ff33;
margin-left: 2px;
text-align: center;
display: inline-block;
}
<div class="stats">
<p>Level: <span id="level">1</span></p>
<progress style="width:150px;background-color:#66ff33;" id="lvelprogress" value="0" max="100"></progress>
<p>Cash: $<span id="cash">50</span></p>
<p>R.A.M: <span id="ram">100</span>MB</p>
<p>Internet Speed: <span id="internet">25</span>MB/s</p>
<P>Hard Drive: <span id="harddrive" >250</span>GB</P>
</div>
<div id="shop" class="shop">
<button class="exit" onclick="hideshop()">X</button>
<p id="uram">Upgrade R.A.M: $<span id="urc">50</span> Level:<span id="url"> 1</span><button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button></p>
<p id="uis">Upgrade Internet Speed: $<span  id="isc">200</span> Level:<span id="isl"> 1</span><button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button></p>
<p id="uhd">Upgrade Hard Drive: $<span id="hdc">100</span> Level:<span id="hdl"> 1</span><button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button></p>
<progress class="upgrade" id="progressBar" value="0" max="100"></progress>
<span id="status"></span>
</div>

Answer №1

Make sure to always assign the LevelValue to 1. var levelhd = 1; However, it's important to utilize the current Level from HTML or store it in a var within your JavaScript code.

I've made a modification to the following line:

var levelhd = parseFloat(hdl.innerHTML);


//statistics
var cash = document.getElementById('cash');
var ram = document.getElementById('ram');
var internet = document.getElementById('internet');
var hd = document.getElementById('harddrive');
var level = document.getElementById('level');
//store
function levelup(t) {
  var bar = document.getElementById('lvelprogress');
  bar.value = bar.value + t;
  t = 0
  if (t == 100) {
    bar.value = 100;
  };
};

function upgradehd(al) {
  var hdc = document.getElementById('hdc');
  var hdl = document.getElementById('hdl');
  var levelhd = parseFloat(hdl.innerHTML);
  var nhd = 250
  var to = level * 20;
  var nhdc = Math.floor(2.5 * hdc.innerHTML);
  var bar = document.getElementById('progressBar');
  var status = document.getElementById('status');
  status.innerHTML = al + "%";
  bar.value = al;
  document.getElementById('lvelprogress').max = 100;
  al++
  var sim = setTimeout("upgradehd(" + al + ")", to);
  if (al == 100) {
    var xp = hdc.innerHTML / 4;
    levelup(xp);
    status.innerHTML = "100%";
    hdl.innerHTML = levelhd + 1;
    hdc.innerHTML = nhdc;
    hd.innerHTML = nhd + 50;
    al = 0;
    status.innerHTML = al + "%";
    bar.value = 0;
    clearTimeout(sim);
  };
};
.update {
  background-color: #66ff33;
  width: 200px;
}
.storage {
  background-color: black;
  color: #66ff33;
  width: 200px;
  border: 5px solid #cccccc
}
.statistics {
  float: right;
  height: 175px;
  width: 150px;
  background-color: #000;
  color: #66ff33;
  margin-left: 2px;
  text-align: center;
  display: inline-block;
}
<div class="statistics">
  <p>Level: <span id="level">1</span>
  </p>
  <progress style="width:150px;background-color:#66ff33;" id="lvelprogress" value="0" max="100"></progress>
  <p>Cash: $<span id="cash">50</span>
  </p>
  <p>R.A.M: <span id="ram">100</span>MB</p>
  <p>Internet Speed: <span id="internet">25</span>MB/s</p>
  <P>Hard Drive: <span id="harddrive">250</span>GB</P>
</div>
<div id="storage" class="shop">
  <button class="exit" onclick="hidestorage()">X</button>
  <p id="uram">Upgrade R.A.M: $<span id="urc">50</span> Level:<span id="url"> 1</span>
    <button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button>
  </p>
  <p id="uis">Upgrade Internet Speed: $<span id="isc">200</span> Level:<span id="isl"> 1</span>
    <button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button>
  </p>
  <p id="uhd">Upgrade Hard Drive: $<span id="hdc">100</span> Level:<span id="hdl"> 1</span>
    <button style="border:2px solid white;color:#66ff33;background-color:#000;" onclick="upgradehd(0);">Upgrade</button>
  </p>
  <progress class="update" id="progressBar" value="0" max="100"></progress>
  <span id="status"></span>
</div>

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

Converting the basic logic from if-else statements to foreach loops was unsuccessful

I am currently working with this function: const applyColor = (value) => { let color //shallow to dark const colors = ['#B3E5FC', '#81D4FA' ,'#4FC3F7', '#29B6F6', '#03A9F4', &ap ...

Is there a way to deactivate a div in Blazor when clicking outside of the div?

Is it possible to have two divs on a single page, where one appears on the right side and the other on the left side? Is there a way to close or hide the left side div whenever we click on the right side div? ...

Upon hovering, icons for each project name are displayed when `mouseenter` event is triggered

My issue lies with the mouseenter function. I am trying to display icons specific to the project name I hover over, but currently, it displays icons for all projects at once. I want each project hovered over to show me its respective icons Below is some c ...

The most sophisticated method for creating a 2x2 grid of divs

I have developed a quiz application similar to Buzzfeed with four answer options for each question. The layout on mobile devices is satisfactory, displayed in a 2x2 grid. However, when viewing the app on desktop screens, the answers appear horizontally in ...

Ways to stop a webpage from auto-refreshing using JAVASCRIPT

While working on a form to submit values using AJAX and PHP, I encountered an issue where the return value of AJAX was always 0. After some investigation, I realized that the problem was caused by the page being refreshed. AJAX var xhttp = new XMLHttpRequ ...

Spinning with animation in jQuery

Greetings! I am in the process of creating a popup box that will appear upon clicking the Login button. My aim is to produce a popup box with a rotating effect, however, my attempts have not been successful so far. You can view the code snippet on JsFidd ...

What is the best way to apply styling to a kendo-grid-column?

Utilizing the kendo-grid in my project serves multiple purposes. Within one of the cells, I aim to incorporate an input TextBox like so: <kendo-grid-column field="value" title="{{l('Value')}}" width="200"></kendo-grid-column> It is ...

Establish restrictions for the minimum and maximum values for each year in the Date Range Picker

After searching for answers to my question, I have found some general solutions but none that fit my specific problem. I created a backend system for users to manage the availability of hotel rooms throughout the selected year. Using HTML/PHP/MySQL, I dis ...

Guide to developing a personalized useReducer with integrated decision-making and event activation

I am interested in creating a custom hook called useTextProcessor(initialText, props). This hook is designed for managing and manipulating text (string) within a React state. It utilizes useReducer to maintain a cumulative state. Here is the implementation ...

What is the best way to establish a page-specific API endpoint in Next.js?

How can I specify the API pathname for different pages in my project? This is the folder structure I am working with: src ├── component │ ├── common │ └── layout ...

Is there a way to access an SD card by clicking on an HTML link using JavaScript?

UPDATE: I am seeking a way to embed an HTML file with JavaScript or jQuery that can directly access the contents of the SD card while being opened in a browser. Currently, I have posted code for accessing it through an activity, but I want to be able to d ...

Ensure that all floating divs have uniform height

How can I ensure that two divs placed side by side will always have the same height, even when one of them changes based on content, using only CSS? I created a fiddle to demonstrate. I want both the red and blue divs to have equal heights... http://jsfi ...

Combining divs with identical values in Angular

I am working on creating my very own custom Calendar. Take a look at the full example of my component here I need help figuring out how to combine week divs that share the same value {{day.weekNumber}} so that there is only one div for each shared value, ...

`I am facing issues with class binding in Vue 3 when using SwiperJS`

Currently, I am working with Vue 3 along with swiperjs. I have encountered a problem related to the v-bind:class behavior in my project. The issue arises when transitioning to the second slide, where the !h-auto class does not get applied as expected. St ...

Add a button feature to both upload and download a todo list within a JavaScript file, similar to a text file

I'm looking to enhance my simple todo list with a download and upload function. I want to be able to save the list in local storage and then upload or download it from a server code. Below is a link to my code: https://jsfiddle.net/zahrashokri/uqkn6t ...

Warning: Validation issue in console.js at line 213 - It is not valid for a <div> element to be nested inside a <p> element. Please correct this nesting validation

react-dom.development.js:86 Warning: validateDOMNesting(...): <p> cannot appear as a descendant of <p>. at p at http://localhost:5173/node_modules/.vite/deps/chunk-WQ5FBX7J.js?v=539aff10:2964:50 at Typography2 (http://localhost:5173/node_module ...

One effective way to utilize await/async within the Vue mounted lifecycle hook is by

I am facing an issue where the mapGetters value is showing null in my computed property because the preferences method is not executed completely. I need to wait until the store has set the getter and setter. I have tried using async/await but it's no ...

What is the process for aligning text with the margin on the left side

Inside a span block, there is an icon: <span><i></i>Text</span> The CSS for the icon is as follows: i { display: inline-block; width: 20px; height: 20px; } When the text inside the span is long, it wraps to the next lin ...

What will occur if I use an XMLHttpRequest to request a file that is currently being downloaded?

My goal is to enhance links in a progressive manner using the PJAX style. My plan was to layer this on top of some regular prefetch <link>s: <link rel="prefetch" href="next.html"/> If the browser has already downloaded next.html, then the PJA ...

Tips for refreshing only a portion of a webpage using JavaScript/jQuery

I have two distinct navigational sections on my website. The left column has its own navigation menu, while the right column (main content area) contains a separate set of links: My goal is to click on a link in the left-hand sidebar (such as "Resume", "E ...