Having issues with my JavaScript timer - seeking assistance in troubleshooting the problem

Trying to set a timer for my little game, but facing some difficulties.

The timer causes the balance to randomly increase from 0 to 13000 + some extra amount.

<script>
var coins = 0; var speed = 1;
</script>
<center>
  <h4>
    Coins Per Second:
    <script type="text/javascript">
      document.write(speed)
    </script>
  </h4>
</center>
<center>
  <p id="coinspersecond">Coins: 0</p>
</center>
<button onclick="minecoins()"><img src="kop.png" height=28 width=32>Mine Coins</button>
<button onclick="stop()"><img src="kop.png" height=28 width=32>Stop Mining Coins</button>

<script>
  function minecoins() {
    coins = coins + speed
    update()
  }

  function update() {
    document.getElementById('coinspersecond').innerHTML = "Coins: " + coins;
    setTimeout(update, 3000)
    minecoins()
  }

  function stop() {

  }
</script>

Answer №1

The `RangeError` you are encountering is due to the continuous loop between `minebtc`, `update`, and `minebtc`. This prevents other code from running, leading to an overflow. A simple solution is to call `minebtc` with a 0ms timer, allowing for interleaving of other events.

<script>
var satoshi = 0; var hash = 1;
</script>
<center>
  <h4>
    Hashs Per Second:
    <script type="text/javascript">
      document.write(hash)
    </script>
  </h4>
</center>
<center>
  <p id="cookiespersecond">Satoshis: 0</p>
</center>
<button onclick="minebtc()"><img src="kop.png" height=28 width=32>Mine BTC</button>
<button onclick="stop()"><img src="kop.png" height=28 width=32>Stop Mining BTC</button>

<script>

function minebtc() {
  satoshi = satoshi + hash;
  setTimeout(update, 3000);
}

function update() {
  document.getElementById('cookiespersecond').innerHTML = "Satoshis: " + satoshi;
  setTimeout(minebtc, 0)
}


  function stop() {

  }
</script>

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

Allow users to edit the textarea only to input values, not from

I am trying to achieve a specific functionality with two input fields and one textarea. Whenever I type something in the input fields, their values are automatically populated in the textarea using .val(). Currently, this feature is working as intended, b ...

Guide on showcasing all entries in the database within the body section of an HTML table

Looking to showcase data from a table inside the body section of an html page This is the code I've been working on: <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="vi ...

Vue component's data remains stagnant within created() hook

I'm currently working on transforming the API response to make it more suitable for constructing two tables. Despite adding debugging outputs within my function in created(), I am witnessing the desired output temporarily, but upon further examination ...

What is the best way to show the probability of users' bets in percentage form based on their wagered amounts?

I am currently working on creating a Jackpot Roulette game that features a main pot. Each round sees users joining and placing bets that contribute to the main pot, with the winner taking home the entire amount. My goal is to provide each user with real-t ...

The userscript is designed to function exclusively on pages originating from the backend, rather than on the frontend in a single-page application

I have a userscript that I use with Greasemonkey/Tampermonkey. It's set to run on facebook.com, where some pages are served from the backend during bootstrapping and others are loaded on the fly in the front-end using HRO, similar to how a Single Pag ...

What is the best way to convert the information from a <SelectInput /> component or similar components into another language?

Within my React admin v3 application, When retrieving data from the servers for my entity, I receive a unique identifier called a slug. This slug is a special key that needs to be translated on the client side. Here is an example of my <CallMeBackCre ...

What is the best way to retrieve a particular div element from a webpage?

I'm having trouble retrieving a specific div element (specifically with the attribute id="vung_doc") from a website. Instead of getting just that element, I seem to be getting almost every element on the page. Any ideas on what could be causing this i ...

Displaying data in JSON format retrieved from a MySQL database

Greetings everyone! I am currently working on a website built with CodeIgniter. In one of my functions, I need to fetch data from MySQL and display the result in JavaScript as part of an Ajax call. Here is my PHP function for retrieving data: public func ...

Construct an HTML tag as a string using Jsoup

I'm faced with an element like this: <p> I am lost </p>` My task is to highlight "am" as follows; <p> I <mark style="background-color:#FFFF00;">am</mark> lost </p> This is my code: String newText = "I <mark ...

Create type definitions for React components in JavaScript that utilize the `prop-types` library

Exploring a component structure, we have: import PropTypes from 'prop-types'; import React from 'react'; export default class Tooltip extends React.Component { static propTypes = { /** * Some children components */ ...

Changing the source of the Carousel image in HTML when it is the active image

I have a unique setup with two carousels on a single page. My goal is to change the src of the first item in the carousel when the second item is clicked. Here is the code for the second carousel item: <div style="max-width: 20%; max-height: 20%;" cl ...

clicking on links to different sections of the page does not automatically bring you to the top of

I am working on a design similar to this: http://jsfiddle.net/MTWu5/ The layout consists of a centered page with a sticky header. The header contains menu links that lead to anchors within the page. My issue arises when clicking on these links - I would l ...

I have to display a pop-up message box after selecting an option from a dropdown menu that fulfills a set of conditions

I am attempting to display a pop-up message when a selection is made on a dropdown menu that meets specific criteria. The dropdown list is generated from a coldfusion output query. I am relatively new to JavaScript, so I may be missing something in my code ...

Having trouble with your UI Router states not updating correctly when changing the URL in your Angular UI Router?

I have tried numerous solutions and tutorials already with no success in finding the right answer. Therefore, I am posting here in hopes of receiving a fresh perspective and a little challenge. Using: angular ui router The Issue I have set up differen ...

Position the SVG next to the link text in a Bootstrap navbar brand

I am using the .navbar-brand class from Bootstrap to include an SVG in my navbar. However, I want to display text next to the SVG and align it properly. The SVG and text are currently in the navbar but they are not aligned correctly. Below is the code snip ...

Struggling to get the jQuery resize event to function properly

Ensuring that my charts remain responsive on different devices has been quite a challenge. Despite implementing a resize event handler in my function to dynamically adjust the charts, I encountered an issue where the page would go blank upon resizing the b ...

What is the best way to retrieve the value of a nested function in JavaScript?

I am currently working on a project that involves a function. function findParentID(parentName) { Category.findOne({ categoryName: parentName }, function (err, foundParent) { var parentID = foundParent.categoryID;    return parentID;<br> } ...

Building on the functionality of AngularJS, a directive scope can be established to access and modify

Can a directive accept and utilize a parameter as its scope value? For instance: angular .module('app', []) .controller('CTRL', function($scope) { $scope.some_value = { instance1: { key1: 'value11', ...

Enhance LCP Performance in Next.js for Faster Loading Speeds

I'm currently working on a next.js project (version 11.1.2) and my goal is to improve the page speed performance. To track this, I am using Google PageSpeed Insight for evaluation. Presently, the scores stand at: 40-50 for mobile (!!) 80-90 for deskt ...