jquery: can you do an ajax request in $(window).unload()?
I am absolutely terrible at logging my hours.
So my idea is to have a browser shortcut that i open when i am starting work.
Then when i am finished close it.
This will then to an ajax request to a php script that will update my hours for me.
To do this.
Can i do an ajax request in $(window).unload()
?
or will the browser close before t开发者_JAVA技巧his completes?
and, will the php script on the other end run?
You can run an synchronous AJAX request (async: false
in $.ajax()
) in window.onbeforeunload
, but an asynchronous one (the default) will most likely be cancelled before it completes. Note that this isn't the same as the unload
event, which is strictly for variable cleanup (though beforeunload
is arguably for the same purpose).
I tend to recommend against doing this, as it delays the user's experience waiting on your AJAX request to complete, when they just want the next page.
Unless you make a synchronous AJAX request, the browser will not wait for the request to finish before closing.
精彩评论