The page price is 126 rubles 00 kopecks. It is necessary to increase this price every minute by 1 ruble. And also for a minute pennies should increase, which show that the price is rising. How can this be implemented? If everything is clear with rubles, we use setInterval and after every 60000ms we save 1, then I can’t figure it out with kopecks.

  • Exactly the same as with rubles, only as usual, to add every 600ms - SLy_huh
  • If you have rubles and kopecks are separate, in different tags, just always increase the penny. It turns out that every second you do + 3 kopecks, or every 2 seconds + 6 kopecks ... As soon as the seconds become equal to 60, reset the kopecks and make +1 rubles. - azhirov1991
  • one
    setInterval does not guarantee exact intervals, you will not sync. Such things always need to synchronize themselves through Date . @Vp_arth has a good answer. - user236014
  • @Anon and why, if you can add a ruble every 100 kopecks? Or is it so important that it is at this minute and second that the ruble jumped, and not a millisecond later / earlier? - SLy_huh
  • one
    @Anon I mean, there is no point in measuring such things for a minute. He will not leave so much that it can significantly affect the result. And to complicate for the sake of this code, well, xs, controversial point. - SLy_huh

4 answers 4

Solution with inaccurate timer.

 var price = 126.00; updatePrice = function() { document.getElementById("price").innerHTML = XFormatPrice(price); price+=(100/60)/100; } updatePrice(); setInterval(updatePrice, 1000); function XFormatPrice(_number) { var format_string = '# руб.'; var r=parseFloat(_number) var exp10=Math.pow(10,2); r=Math.round(r*exp10)/exp10; rr=Number(r).toFixed(2).toString().split('.'); b=rr[0].replace(/(\d{1,3}(?=(\d{3})+(?:\.\d|\b)))/g,"\$1"+' '); r=(rr[1]?b+ ',' +rr[1]:b); return format_string.replace('#', r); } 
 <div id="price"></div> 

  • it was said Если с рублями все понятно, то с копейками не могу сообразить. - SLy_huh
  • @SLy_huh was inattentive, corrected. - ilyaplot
  • penny current jump. If you update every second, you get 60 updates per minute, during which time the ruble is added - 100 kopecks. Those. 40/100 user will not see updates - SLy_huh
  • @SLy_huh and where is the condition that the user should see all updates? - ilyaplot
  • one
    Math.pow(10, 2) is 100 ? I'm not confusing anything? I would write 1e2 - vp_arth

It is necessary to anchor the moment in time (the beginning of the countdown) and link it with the price.
Then you can often call the conversion function, and update the price, depending on the time passed from the beginning.

 var costEl = document.getElementById('cost'); var velocity = 1/60; // рубль в минуту var start = Date.now(); var startCost = parseFloat(costEl.innerText); setInterval(function(){ var now = Date.now(); var secs = (now - start) / 1e3; var newCost = (startCost + secs*velocity).toFixed(2); costEl.innerText = newCost; }, 100); 
 <span id="cost">100.00</span> 

  • I am sorry, do not prompt why you divide secs into 1000? I already broke my head, I see what works, but I can’t fully understand how .. - Telion
  • Date.now() returns the current time in milliseconds - vp_arth
  • Oh, right, thank you :) - Telion

 var velocity = 100; setInterval(function(){ var h = $('#hundredths').text(); var r = $('#roubles').text(); if(++h == 100){ h = 0; r++; } $('#hundredths').text((h < 10 ? '0' : '') + h); $('#roubles').text(r); }, velocity); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Цена, руб. <span id='roubles'>126</span>.<span id='hundredths'>00</span> 

  • All that I don’t like in this answer is the inner perfectionist) Still +1. Well and still, it would be desirable a way to regulate speed) - user236014
  • @Anon thanks. I just don’t like bulky solutions for simple tasks. Speed ​​on the advice also added =) - SLy_huh
  • one
    &#8381; - I understand the symbol of the ruble? I bring to your notice that I do not see it) Ubuntu, Chrome - user236014
  • 2
    @Anon, okay, this will also be removed, yet we are fighting for perfectionism, which means everything should be displayed everywhere to the maximum =) - SLy_huh

Use operator % - remainder of division:

Test (accelerated):

 var x=0 setInterval(function(){ x++ console.log(x%100) },60000/1000) 

  • console.log may be slower than you require from it) - vp_arth