Search code examples
javascriptpythonpromiseskulpt

How to stop a script in skulpt


I made a skulpt project for my students. Every time they made an infinite loop, the browser crashes and the code is gone. That is very frustrating for the students. I saw a stop button on https://trinket.io/python, but don't understand how they made it. I followed the instructions on http://www.skulpt.org/ to integrate skulpt in my site.

Any ideas?


Solution

  • Not particularly well documented - there are certain options that you can add to the Sk.configure one of which is execLimit in ms.

    e.g. From the example http://skulpt.org/using.html

    Sk.configure({output:outf, read:builtinRead, execLimit: 5000}); 
    
    

    Skulpt will raise a TimeLimitError if the execLimit is reached.


    in terms of the cancel button - there's an additional setting yieldLimit which you can set in ms. This will essentially pause the execution at intervals. And you can add a handler to deal with this pause.

    Again not documented well by Skulpt but here's a JS Fiddle that is proof of concept:

    https://jsfiddle.net/bjLuq5s7/