When does javascript stop running on a page after a link is clicked?

前端 未结 2 1308
南方客
南方客 2021-02-20 02:41

I have a page which runs various javascript code, including calling setTimeout(). If a user clicks a link to navigate to another page, at what point does the javasc

相关标签:
2条回答
  • 2021-02-20 03:06

    One possible solution would be to stop the click event, fire away an asynchronous call and resume the default event when the result from the ajax call has been processed. Or, if possible, you could handle the processing on the server in which case you wouldn't even need to stop the default event - just fire your asynchronous call.

    In answer to your question, I agree with Jack that the script would stop run after the onunload event.

    Hope this hepls.

    0 讨论(0)
  • 2021-02-20 03:07

    Here's the sequence of what happens:

    Step 1: You click on a link. Here the browser sends a request to the server and waits for the response. The current page's document object still exists.

    Step 2: Browser receives the response. The current page's document objects still exists.

    Step 3: The browser parses, creates and renders the response to a new document object.

    Step 3 is when the current page's JS will stop, UNLESS, you have targeted the response to an iframe, for example. If you do that, then the new document object will be rendered inside the iframe and the current page's document object remains intact and JS will still remain functional.

    Hope that answers your question!

    0 讨论(0)
提交回复
热议问题