JavaScript Update variable based on time passed

I currently have the following code:

<div id="resource1"></div>
<div id="resource2"></div>
<div id="resource3"></div>


<script>

var resource1 = 50;
var resource2 = 50;
var resource3 = 50;

var modifier = 3;

function resourcesupdates() {
    setInterval(function() {
            resource1 = resource1 + modifier;
            resource2 = resource2 + modifier;
            resource3 = resource3 + modifier;
            document.getElementById('resource1').innerText = resource1;
            document.getElementById('resource2').innerText = resource2;
            document.getElementById('resource3').innerText = resource3;

            console.log(resource1);

     }, 1000);
}

resourcesupdates();
</script>

This code works because ever second the resources are updated by 3 per second.

What if I wanted to have it update at a rate of 47 per minute, however I wish for it to output to the user only when the resource is updated by a whole number, not every second as that would result in decimal values?

Answer:1

As stated in my comment above: The problem is, that when updating by 47/60 per second, you might never reach a point where the number has no decimal places. If it still meets your requirements when the number is just always rounded for display, this could be your solution:

var resource1 = 50;
var resource2 = 50;
var resource3 = 50;

var modifier = 47; // per minute

function resourcesupdates() {
    setInterval(function() {
            resource1 = resource1 + modifier/60;
            resource2 = resource2 + modifier/60;
            resource3 = resource3 + modifier/60;
                        
            document.getElementById('resource1').innerText = Math.round(resource1);
            document.getElementById('resource2').innerText = Math.round(resource2);
            document.getElementById('resource3').innerText = Math.round(resource3);
     }, 1000);
}

resourcesupdates();
<div id="resource1"></div>
<div id="resource2"></div>
<div id="resource3"></div>
Answer:2

I want to output "You are logged in as [Username]" using i18next for the text and a Jade variable as the actual username, but I can't seem to get them working together. As far as i can tell, i18n ...

I want to output "You are logged in as [Username]" using i18next for the text and a Jade variable as the actual username, but I can't seem to get them working together. As far as i can tell, i18n ...

The following code passes an argument variable nextId to the IndexedDB Store get method and the corresponding records are returned if the argument passed matches the key of the record. Initially I ...

The following code passes an argument variable nextId to the IndexedDB Store get method and the corresponding records are returned if the argument passed matches the key of the record. Initially I ...

I'm quite new to javascript and can't find the solution. I have the following code. <form class="edit-task-form"> <legend>Create Task</legend> <label>Task</...

I'm quite new to javascript and can't find the solution. I have the following code. <form class="edit-task-form"> <legend>Create Task</legend> <label>Task</...

Any help converting Excel "power of" function to JavaScript? Values are: A1 = 99% A2 = 45 formula is = (A1^A2)*100 results rounded to whole number is 64.

Any help converting Excel "power of" function to JavaScript? Values are: A1 = 99% A2 = 45 formula is = (A1^A2)*100 results rounded to whole number is 64.

  1. excel power of 2
  2. excel power of 10
  3. excel power of e
  4. excel power of 3
  5. excel power of symbol
  6. excel power of 2 formula
  7. excel power of function
  8. excel power of number
  9. excel power of two
  10. excel power of 4
  11. excel power of n
  12. excel power of negative number
  13. excel power of 2 symbol
  14. excel power of
  15. excel power of matrix
  16. excel power of calculation
  17. excel of power bi
  18. excel axis power of 10
  19. excel formula power of x
  20. excel formula power of n