3

I want to run some JS code from a string, and I want to set a timeout for this code or stop eval in case of exceptions in eval.

my code:

function run_code() {
    let code = editor.getValue();
    eval.call(null, code);
}

and if I put some infinite loop in code, the browser freezes forever:

function run_code() {
    let code = "while (true) { console.log('Q'); }";
    eval.call(null, code);
}

Is there a way to call eval with a timeout, or kill the existing eval with JS? How should I properly call eval to be able to stop it if I want to?

Thank you!

3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.