How do I detect and stop an infinite loop in user-provided JavaScript?

I am writing an editor in a browser for games. The editor will allow users to write their own JavaScript files, which are then loaded into the same DOM as in the editor. This will allow them to see the game in a canvas next to the code and update it every time they save.

The editor is for people who are not familiar with JavaSript and it can be easy to accidentally end up with an infinite loop. If possible, I want to set up my editor in such a way that if the loop runs too many times or too fast, the loop breaks and a message pops up warning the user that they have an infinite loop.

This would be ideal for people who are new, because if the whole editor just crashed suddenly, they might not understand what their mistake is and think it's a problem with my editor.

How can I implement fail-safe functions in my editor that stop infinite loops automatically?

+3


source to share





All Articles