/    Sign up×
Community /Pin to ProfileBookmark

JavaScript counter

I am trying to build a counter that counts to 10 and then when it hits 10 goes back to 0 but it counts every 500ms.
Like 0 /500ms/ 1 /500ms/ 2 /500ms/ 3 etc.

I have some problems with the counter. Here is the code.

“`
let counter = 0;

function countIt() {

if(counter <= 10) {
setInterval(() => {
counter++
}, 500)
} else {
counter = 0;
}
}

countIt();

console.log(counter)
“`

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumOct 11.2021 — Two options:
  • 1. Use a status variable, values 'up' or 'down', and count up or down and check boundaries according to the status.

  • 2. Use a variable for the delta and switch it's values from 1 to -1 and vice versa. Add this value to the current counter value.


  • Edit: Apparently I misunderstood what your intention is. You have to set the counter back to 0 **inside** the callback function:
    ``<i>
    </i> function countIt() {

    setInterval(() =&gt; {
    console.log(counter);
    if (counter &lt;= 10) {
    counter++
    } else {
    counter = 0;
    }
    }, 500)
    }<i>
    </i>
    ``
    Copy linkTweet thisAlerts:
    @cootheadOct 11.2021 — Hi there RaulRogojan,

    try it like this, perhaps...

    &lt;script&gt;
    (function() {
    'use strict';

    let counter = 0;
    let counter2 = 0;

    function countIt() {

    counter2 ++;

    if ( counter &lt;= 10 ) {
    console.log( counter );
    counter ++;
    }
    else {
    counter = 0;

    }
    if ( counter2 &gt; 22 ) { /* 11 = 1 iteration, 22 = 2 iterations */
    clearTimeout( si );
    }
    }

    let si = setInterval(
    function(){
    countIt();
    }, 500 );

    }());
    &lt;/script&gt;


    [i]coothead[/i]
    Copy linkTweet thisAlerts:
    @boohooOct 11.2021 — Does 10 ever show or not?

    ``javascript<i>
    </i>let counter = 0;

    function countIt() {
    if (counter &lt;= 10) {
    setTimeout(() =&gt; {
    counter += 1;
    countIt();
    }, 500);
    } else {
    counter = 0;
    }
    }

    countIt();<i>
    </i>
    ``
    Copy linkTweet thisAlerts:
    @codyhillauthorOct 12.2021 — @coothead#1638112 Thank you!
    Copy linkTweet thisAlerts:
    @codyhillauthorOct 12.2021 — @boohoo#1638116 Yes, but this gives me undefined
    Copy linkTweet thisAlerts:
    @codyhillauthorOct 12.2021 — @coothead#1638112 The problem is that I need the counter to be accessible as a global scope.

    Maybe there is a better way of doing this but I want a function to be called every 10 seconds.

    I have an API that throws data very fast but I want to see only the last few data changes every 10 sec or so.
    Copy linkTweet thisAlerts:
    @boohooOct 12.2021 — @RaulRogojan#1638122 what gives you undefined?

    You probably don't want setInterval() here, because setTimeout() fits better. You don't need to clear it.
    Copy linkTweet thisAlerts:
    @codyhillauthorOct 12.2021 — @boohoo#1638130 betTimeout runs only once
    Copy linkTweet thisAlerts:
    @boohooOct 12.2021 — @RaulRogojan#1638134 yes, but it's a common pattern to run setTimeout() recursively. In practice it's much more popular than setInterval() which requires a hassle of remembering the ID.
    ×

    Success!

    Help @codyhill spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...