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 7673655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:33:45+00:00 2026-05-31T16:33:45+00:00

I have an event, which can fire itself. I try to make the code

  • 0

I have an event, which can fire itself. I try to make the code as efficient as possible, but it can hit maximum call stack in some circumstances, which are out of my control. It’s not an infinite stack and it will end at some point, but sometimes it can potentially crash before it finishes because of the limit.

Will I increase the number of call stack if I set up 2 similar event listeners and split the code? Or what can I do?

UPDATE: It’s on DOM change event (working with Webkit only, so don’t care about other browsers), which can also modify the DOM based on some conditions. I haven’t really hit that limit yet, but theoritically, it potentially can. I’m still optimizing the code to make as less DOM manipulations as possible.

UPDATE 2: I’m including sample (not real) example:

document.addEventListener('DOMSubtreeModified', function(event){
            
    this.applyPolicy(event);
            
}, true);

function applyPolicy(event){
    if( typeof event != "undefined" ){
        event.stopPropagation();
        event.stopImmediatePropagation();
    }
    
    if( !isButtonAllowed ){
        $('button:not(:disabled)').each(function(){
            
           $(this).attr('disabled', true);

        });
    }
}

This is just a sample code, but even in this case, if you have say 100s of buttons, the call stack will be in 100s too. Note that if you use $('button').attr('disabled', true);, this will cause call stack problem, because jQuery will be trying to modify the DOM infinitely.

  • 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-05-31T16:33:46+00:00Added an answer on May 31, 2026 at 4:33 pm

    While it sounds like you may need to rethink some code, one possibility would be to put a recursive call in a setTimeout at some given interval. This allows you to begin a new call stack.

    Take this example…

    var i = 0;
    
    function start() {
        ++i;
        var is_thousand = !(i % 1000);
    
        if (is_thousand)
            console.log(i);
    
        if (i >= 100000)
            return; // safety halt at 100,000
        else
            start()
    }
    

    It just logs to the console at every interval of 1,000. In Chrome it exceeds the stack somewhere in the 30,000 range.

    DEMO: http://jsfiddle.net/X44rk/


    But if you rework it like this…

    var i = 0;
    
    function start() {
        ++i;
        var is_thousand = !(i % 1000);
    
        if (is_thousand)
            console.log(i);
    
        if (i >= 100000) // safety halt at 100,000
            return;
        else if (is_thousand)
            setTimeout(start, 0);
        else
            start();
    }
    

    Now at every 1,000, the function will be allowed to return and the next call will be made asynchronously, starting a new call stack.

    Note that this assumes that function is effectively ended when the recursive call is made.

    Also note that I have a condition to stop at 100,000 so we’re not infinite.

    DEMO: http://jsfiddle.net/X44rk/1/

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

Sidebar

Related Questions

Let's say I have a class A which can fire an event called X.
We have some legacy code that needs to identify in the Page_Load which event
I have code in the global.asax file's Application_Error event which executes when an error
Can we have same delegate for two events ? which have same number of
I have an event which needs to contact some third party providers before performing
Suppose I have a event handler which makes two AJAX calls to the server:
I have a sharepoint event handler which I want to activate for a single
I have a WCF server and I'm tapping into the Faulted event which stupidly
I have a List<T> where T is my Event type which has a field
I have an Oracle table which contains event log messages for an application. We

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.