Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:10:38+00:00 2026-06-15T01:10:38+00:00

I was asked (by a friend) to build a timer (infinite one which writes

  • 0

I was asked (by a friend) to build a timer (infinite one which writes a line every second), but without setInterval.

I solved it with :

var i = 0;

    function k(myId, cb)
    {
        setTimeout(function ()
        {
            console.log(myId);
            cb();
        }, 1000);
    }

    function go()
    {
        i++;
        k(i, go);
    }

    go();

And it is working.

The problem is that I’m afraid there’s gonna be a memory pressure. It actually creates a recursion and after a while (week or something) – the process will consume much memory. (the stack is never deallocated)

How can I change my code in order not to be much memory consume?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:10:39+00:00Added an answer on June 15, 2026 at 1:10 am

    It’s not recursion

    It may look like recursion, but setTimeout does not create recursion.

    The way setTimeout works is that it returns immediately. So the call to k ends immediately with its stack deallocated.

    When the timeout actually happens and the call to go happens again it is not from the point of the previous call to k but from the global scope*.

    * Note: I’m not using the strict meaning of scope as defined in ECMAScript spec here. What I mean is the call to k will be made as if you have written it in a plain <script></script> tag: that is to say, outside of any other function calls.

    Regarding your concern over the closure

    In your specific case, there is very little that’s actually enclosed in the closure created by the k function. The only significant closure is the reference to the arguments cb and myId. And even then it only lasts for approximately one second:

     #1   function k(myId, cb) {
     #2        setTimeout(function(){
     #3            console.log(myId); // there is a closure here to myId
     #4            cb();              // and another one for cb
     #5
                 /* But at this point in the function, setTimeout ends
                 * and as the function returns, there are no remaining
                 * references to either "cb" or "myId" accessible
                 * anywhere else. Which means that the GC can immediately
                 * free them (though in reality the GC may run a bit later)
                 */
      #6       }, 1000); // So one second is roughly the longest the closure lasts
        }
    

    Could be simpler

    I should note that your code is fairly convoluted. It can be written simpler, and without using closures at all (minus the global variable i) if you simply write it like this:

    // Simpler, does exactly the same thing:
    var i = 0;
    function go () {
        console.log(i);
        i++;
        setTimeout(go, 1000); // callback
    }
    go();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked my friend if he can print from 1 to 1000 without using
I have been working on something which a friend asked me. He is using
I have quite literally tried every combination and asked a friend for the reason
Possible Duplicate: Should C# include multiple inheritance? One of my friend asked me the
MY friend asked here a question And the one who answered him - wrote
This is just a random question my friend asked me which I also do
My friend asked me if it is possible to build a web site like
I don't have much experience with Android, but was asked by a hearing-impaired friend
I asked this question already but my friend said to take a different approach.
A friend asked how in SQL to convert a varchar which represents an octal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.