简体   繁体   中英

How to make timer reach certain value after specific amount of time?

Okay, i made a simple timer and i want to reach certain value after some time. Let's say it starts from 100 and it need to reach 300 in 2 mins and in the meantime it increments and decrements by 5 for example and keeps up to create the illusion that it's random and not just straightforward incrementation. In the end it will reach 300 for the same time. Similar to social blade real time subscribing. It adds and removes certain amount of subscribers because people subscribe and unsubscribe but here it will reach the end value in 2 or 5 min.

 let count = 0; let end = 300; function increment() { if (count < end){ count++; }else if(count >= end) clearInterval(count); document.getElementById("num").innerHTML = count; } setInterval(increment, 1000);
 <html> <body> <h2 id="num"></h2> </body> </html>

I solved this problem.

function subscribeEmu (sub, unsub, startCount, time);

takes 4 parameters:
sub - number of subscribers
unsub - the number of unsubscribed
counter start value
time - duration in milliseconds

here you can run.

thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM