How to stop JavaScript eval

Viewed 1027

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!

0 Answers
Related