3

Sometimes I make mistakes and get infinite loops in JavaScript (example: while(true){}). Firefox helpfully reports "Error: Script terminated by timeout" or that the memory allocation limit is exceeded. How can I change the length of this timeout? I want to shorten it during development.

I could also solve this problem by writing a function that I call inside the loop that counts iterations and throws an exception on too many iterations. But I'm also interested in how to change the timeout value.

I have not seen this timeout documented, and have not found it by searching.

David Spector
  • 1,520
  • 15
  • 21

5 Answers5

1

Unfortunately the maximum recursion limit is not user configurable from within a page running javascript. The limits also vary across browsers.

This Browserscope test showcases the results of user testing from another StackOverflow question on the subject: What are the js recursion limits for Firefox, Chrome, Safari, IE, etc?

Aside from writing your own timeout or utilising a promise chain to process data, you won't be able to change the timeout yourself.

samthecodingman
  • 23,122
  • 4
  • 30
  • 54
0

There are multiple ways to change the timeout of infinite loops.
One shorter method is setInterval:

setInterval(function() {
   document.body.innerHTML += "Hello!<br>";
}, 1000) //This number is, in milliseconds, the interval in which the function is executed.

Another better method is window.requestAnimationFrame. This gives you a higher quality animation(see Why is requestAnimationFrame better than setInterval or setTimeout for more details); here's an example of it in motion:

function run() {
    setTimeout(function() {
        window.requestAnimationFrame(run);
        document.body.innerHTML += "Hello!<br>";
    }, 1000); // This sets an interval for the animation to run; if you take the setTimeout out the function will loop as quickly as possible without breaking the browser.
}
run();
Sarah
  • 460
  • 4
  • 9
0

D. Joe is correct. In Firefox, browse to about:config and change the number of seconds value in dom.max_script_run_time .

See http://kb.mozillazine.org/Dom.max_script_run_time for details--you can also eliminate the timeout.

David Spector
  • 1,520
  • 15
  • 21
0

This will partially answer your question, but here is how I handle such case. It is more a workaround than a fix.

IMPORTANT: Do not put this code in production environment. It should only be used in local dev while debugging.

As I tend to be debugging when I stumble upon this kind of case, I am mostly likely using console.log to output to console. As such, I override the console.log function as follow anywhere near the entry point of my app:

const log = console.log

const maxLogCount = 200
let currentLogCount = 0
console.log = function (...msg) {
  if (currentLogCount >= maxLogCount) {
    throw new Error('Maximum console log count reached')
  }

  currentLogCount++
  log(...msg)
}

Then when I accidentally do this

while (true) {
    console.log("what is going on?")
}

It will error out after 200 outputs. This will prevent the tab from locking for half a minute and having to reopen a new tab and bla bla bla.

Lunfel
  • 2,499
  • 21
  • 29
-1

It is usually just the browser's way of saying the script is out of memory. You could solve this by using for loops or creating and index variable and adding to it each time like so:

var index = 0;
while(true){
if(index > certainAmount){
break;
}

index++;
}

If you really want something to go on forever read about setInterval() or setTimeout()

IbzDawg
  • 79
  • 7