E
E
Eugene Chefranov2020-04-27 16:32:26
JavaScript
Eugene Chefranov, 2020-04-27 16:32:26

How to simulate a view counter?

How can I make a fake view counter? Let's say there will be two variables where the range from and to is set. And for example, every minute this number on the page will be updated within the range.
And you can somehow make the number by which the increase / decrease was not sharp. Let's say the range is from 50 to 200 and first a random number is 75, and after a minute 200, so that the plus or minus from the previous value is, for example, 5, and not such a big jump.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bkosun, 2020-04-27
@Chefranov

More or less like this:
<span class="counter">100</span>

let counter = $('span.counter');

    setInterval(function () {

        let min = (Math.random() > 0.5) ? 0 : 5;
        let max = (Math.random() > 0.5) ? 5 : 10;

        let c = parseInt(counter.text());
        let r = Math.floor(Math.random() * (max - min + 1)) + min;

        if (Math.random() > 0.5) {
            counter.text(c + r);
        }

    }, 2000);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question