开发者

Javascript keep running even when you refresh the page

Have anyone of you experienced that a javascript in a page keep running even after you refresh the page? As far as I know, javascript is single threaded, and it shouldn't keep running if I refresh the page.

For you info, I was imp开发者_高级运维lementing comet for a website. The previous request for lpoll keeping posting to server even after I refresh the page.


There are two stages to a navigation, whether that navigation is the result of following a link, or hitting ‘Refresh’.

When you first start the navigation, the page is not immediately killed. The onbeforeunload event will fire, and JavaScript will continue to run normally, including sending AJAX requests. The browser sends an HTTP request for the new page URL and waits for a reply.

Maybe the server will send back a 204 Not Modified response. In that case, the old page will just carry on running. Maybe there'll be some redirects to follow, slowing it down further. Anyway, it could take a while before the browser receives enough of the new HTML page response to start rendering it.

Only then is onunload fired, the previous page killed, AJAX and any other outstanding requests from it aborted, and the new page displayed. At this point your old script definitely won't be running.


When you refresh the page, everything is re-loaded including html dom, javsscript, css, etc. So you can't keep it running when you refresh the page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜