All Collections
Coding Sketches
My sketch got stuck in an infinite loop, how can I fix it?
My sketch got stuck in an infinite loop, how can I fix it?

The infinite loops happen to the best of us. Learn how to recover a frozen sketch due to these nasty loops.

Sinan Ascioglu avatar
Written by Sinan Ascioglu
Updated over a week ago

Loops such as for() or while() are fun, but they can get stuck if a condition that is supposed to break the loop never happens. Browser freezes in the limbo of infinity, computer fans start running like launching into the space!

Here is a while loop example:

var index = 0;
//..your code here
//..that forgets to increment "index"

And a self-calling function:

function myFunction(){
//..your code here

We provide three features to prevent losing your progress due to these loops.

  • Loop protection feature will inject some code to track your loops behind the scenes when you run your sketch, and it will stop it if a loop takes more than 5 seconds. When it does, you will see a warning in the console as below and you will be dropped back to your code to fix the issue.

    This feature is enabled by default in the sketch settings but you may want to disable it if it causes problems with your sketch. Or, you can add "//noprotect" to the loop line in your code to tell it not to track a particular loop.

    while(index<10){ //noprotect
    //..your code here

  • We also save the latest state of your code in your browser cache. In cases that loop protection doesn't catch an infinite loop (e.g. self-calling functions) and your browser kills the tab, simply refresh the page and our editor will recover the latest state and let you know.

  • In any case, you can always access the code directly with "#code":
    - Simply copy the URL of your sketch,
    - Open a new browser tab
    - Paste the URL and add "#code" to the end of the URL (e.g.
    Our editor will skip running your sketch and it will directly drop you into the code to fix the issue.

Did this answer your question?