Im just a newbie in Javascript but I have some serious problem which I searched for many hours on different sources, so I decided to post this question.
How do you stall/pause a javascript before an asp.net page is refreshed or closed? Here is the code:
<script type="text/javascript">
window.onbeforeunload = confirmExit;
function confirmExit() {
var connection = new Ext.data.Connection({
defaultHeaders: {
'Content-Type': 'application/json'
}
});
connection.request({
url: 'services/vault/deletewfinstace/'
});
refresh();
//alert("Helloo");
}
function refresh() {
setTimeout(function () { window.location.reload() }, 6000);
}
</script>
In this code I have called a javascript function on the event: window.onbeforeunload, and tried to put a setTimeout() after the function has done its work, but still page refreshes or closes without completing the contents of the function properly. The contents inside the function will execute successfully when I use alert() at the end of the function, but it is somewhat annoying to the user. Please help. Thank you.
I imagine that connection.request
is asynchronous. When you send the request the JS runner will be free, causing the window to reload. Try making the request synchronous if you want the browser to wait before reloading.