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

  • SEARCH
  • Home
  • 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 8229691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:52:59+00:00 2026-06-07T16:52:59+00:00

This is node.js. I have a function that might become an infinite loop if

  • 0

This is node.js.

I have a function that might become an infinite loop if several conditions are met. Untrusted users set these conditions so for the purpose of this question please assume the infinite loop is unfixable.

Still I need a way to stop the infinite loop.

Here is some sample code for what i’m trying to do:

var infiniteloop = false;
var condition = true
function loop () {
  while (condition) {
    console.log('hi')
    if (infiniteloop) {
      condition = false
      console.log('oh last one')
    }
  }
}

loop()

So a few questions based on what I’m trying to do.

  1. If the infiniteloop variable is set to true, the loop will stop right?
  2. How do I detect the infinite loop? Something that checks every 3 seconds would be good.
  3. The infiniteloop variable cannot be changed while it’s looping if it’s on the same process. I have to store the variable in a different process?
  4. Whatever detects the infinite loop needs to live in a different process? Ideally same process would be nice but whatever works?

Thanks for your help.

  • 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-07T16:53:01+00:00Added an answer on June 7, 2026 at 4:53 pm

    A solution based on a mix of the other proposals:

    function Worker()
    {
        this.MaxIterations = 1000000;
        this.Enabled = true;    
        this.condition = true;
        this.iteration = 0;
        this.Loop = function()
        {
            if (this.condition 
                && this.Enabled 
                && this.iteration++ < this.MaxIterations)
            {
                console.log(this.iteration);
                setTimeout(this.Loop.bind(this),0);
            }
        };  
        this.Stop = function()
        {
            this.Enabled = false;
        };
    }
    var w = new Worker();
    setTimeout(w.Loop.bind(w), 0);
    setTimeout(w.Stop.bind(w), 3000);
    

    Not sure this is optimal, but that should work as expected.

    The use of setTimeout to resume the loop allows the main node.js event loop to process other events, such a w.Stop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Expressjs w/ node.js and I have a route like this: users.init(app.db, function()
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have this function in C: static Node* newNode(void* e){ Node n={e,NULL,NULL}; return &n;
I have a invalid XML like this Warning: count() [function.count]: Node no longer exists
I have a data object that looks like this: { 'node-16': { 'tags': ['cuda'],
I have a struct that takes a function pointer, like this: typedef int (*node_transition_func)(
I have this function that I'm using to open a modal dialog that I
I have a function that inserts a new node at the tail end of
I have this function that I need to return true or false. Problem is,
i have this problem to find a particular xml node l have post this

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.