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.
4 answers
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)is100? I'm not confusing anything? I would write1e2- 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
secsinto 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
₽- 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.logmay be slower than you require from it) - vp_arth
|
setIntervaldoes not guarantee exact intervals, you will not sync. Such things always need to synchronize themselves throughDate. @Vp_arth has a good answer. - user236014