This question has already been answered:

Hello. I just started to learn javascript, and in this process there was a little problem that at the moment I cannot solve.

So 24h is given, the initial price (say 1000), the final price (say 6000). -> difference (5000).

The goal is to get a function that will increase the price (every second) starting from 1000 and ending with 6000 (by 5000) until these 24 hours pass. All the time manipulations I do in the format. I think time is so

var countDownDate = new Date($timestamp['created_at')); countDownDate.setHours(countDownDate.getHours() + 24); var now = new Date().getTime(); var distance = countDownDate - now; 

Krivorukost does not allow me to find decent information on this issue in Google, for this I ask you to kick links, possible solutions, etc., etc. Thanks in advance!

Reported as a duplicate by members vp_arth , user194374, Sasha Omelchenko , m9_psy , Yuri 27 Feb '17 at 15:03 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • 2
    Just do setInterval (), in which every second you add the value you need, after having calculated it by the formula. For example, in one hour, 3600 seconds, you need to increase the price by 1000 in an hour. Then 3600/1000 = 3.6. Everything. setInterval () you start to add 3.6 every second and check if the end price is reached, then let it stop. - Bim Bam

1 answer 1

 var minPrice = 1000; var maxPrice = 6000; var price = minPrice; var delta = (maxPrice - minPrice) / 3600; function setPrice() { document.getElementById("price").innerHTML = price; price += delta; if (price > maxPrice) price = minPrice; price = Math.round(price * 100) / 100; } setPrice(); setInterval(setPrice, 1000); 
 <div>Price: <span id="price"></span></div>