0

On my site, a user can control the JS on the page (through some security constraints, not worth explaining the details in the question). Occasionally, they can accidentally create a JS process that will freeze the window as it runs.

Is there anyway a user can click a button, and it will "kill" any actively executing JS functions?

1
  • 1
    If the window is already frozen, the button won't have any effect, right? Commented Apr 9, 2014 at 19:48

2 Answers 2

4

No.

Javascript is single threaded, so it can only do one thing at a time. This is exactly why your page is frozen while it's running. You can't handle the press of a button with javascript if the javascript is already running some code.

You should probably rethink your approach so that this scenario isn't even possible, as you seem to searching for a workaround for a serious flaw in the design of your software.


A possible way to solve this is to use Web Workers. These are background processes that can execute in their own thread. They can do intense calculation without blocking the functionality on your page.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Alex, this is what I feared just figured I'd check (will accept when timer runs up). Is it possible to have an overall timer that checks how long a js function has been running, and if it's over X time, stop all js? This also seems impossible with single threading.
Nope. You'd have to build that into the code that is running long, checking how long it's been since it's started every so often.
1

They can reload the page. If you keep track of the context your JS is in (with localstorage or cookies), you could reset that context after reloading so it would appear to the user as an interrupt.

Comments

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.