Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to create a dynamic live counter

JOKKER

New Coder
I'm trying to create a dynamic live counter.

The counter needs to:
  • have some text before and after.
  • change live.
  • stay between 1 and 10.
  • change only by ±1.
  • have 2 to 10 second intervals between each update (Random but no longer than 10 and no less than 2 seconds between each update).
  • if possible, be displayed globally, not individually (All website visitors should see the same exact number at the same exact time).
Here's what I have so far:

HTML:
<html lang="en">

<head>
  <meta charset="utf-8">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
  <div id="counter-area">I have a <span id="counter"></span> inch pencil</div>
</body>

<script>
  function r(t,r){return Math.floor(Math.random()*(r-t+1)+t)}var interval=2e3,variation=1,c=r(1,10);$("#counter").text(c),setInterval(function(){var t=r(-variation,variation);c+=t,$("#counter").text(c)},interval);
</script>

</html>

1. It does not stay between 1 and 10. I thought c=r(1,10) would set the min and max but it doesn't. How can I prevent the counter from going above 10 and below 1?

2. It changes at a constant rate of 2 seconds because of interval=2e3. How can I set the interval to be random but no longer than 10 and no less than 2 seconds between each update?

3. It is displayed individually, meaning every customer sees a different number and refreshing the page restarts the counter from a random number. Is it possible to display the counter globally?

4. It doesn't work when I add it to my website. For some reason ,$("#counter").text(c)},interval); causes an error and the counter does not work. The error says "Uncaught TypeError: $ is not a function". (Screenshot of the error). What does it mean and how do I fix it?
 
Hello!

1. What values does it return? Looks like it should work.
2. Set the interval to r(2, 10).
3. You would have to use a server-side language such as PHP.
4. What is '$' supposed to do?
 
Hello!

1. What values does it return? Looks like it should work.
2. Set the interval to r(2, 10).
3. You would have to use a server-side language such as PHP.
4. What is '$' supposed to do?
Hi,

1. The counter starts at the correct value (between 1 and 10) but it does not stay in this range. It goes above 10 and below 1 (into the negatives even).

2. I set the interval to interval=r(2e3, 1e4) but it does not stay between 2 and 10 seconds.

3. Do you know how to create such a counter using PHP?

4. I have no idea. I put this code together from multiple different peaces of code. It works when I test it at CodePen but doesn't work when I add it to my website.

Here's the pen so you can see how it works for yourself.
 
1. The problem is probably not with the function 'r'.
2. It is normal, 2e4 equals 2000*** and 1e4 equals 10000***, so the range is [200, 1000].
3. Yeah, but it takes some work.. Don't you know PHP? You should learn it.
 
Last edited:
2. It is normal, 2e4 equals 200 and 1e4 equals 1000, so the range is [200, 1000].
The time is in milliseconds not seconds. So 2e3 = 2000ms = 2s and 1e4 = 10000ms = 10s. The range is correct but it still doesn't stay between 2 and 10 seconds.

3. Yeah, but it takes some work.. Don't you know PHP? You should learn it.
I don't. I also don't plan on being a programmer so learning a whole programming language for a dynamic counter would be unreasonable.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom