I have a page where I show 5 questions to a user and when he clicks on Next 5 link I am sending the score of current page onbeforeunload()
to the script updateScore()
asynchronously using jQuery AJAX and when the call is successful the next 5 questions are displayed.
window.onbeforeunload=function()
{
$.ajax({
type: "POST",
url: "updateScore.php",
data: "pageScore="+score,
cache: false,
timeout:5000,
async:false
});
}
But the problem is that on slow connections,it might hang the browser for a while until AJAX call returns successfully.When I tried async:true
(default) the next page is loaded first without making call to updateScore.php
.It might be due to the fact that connection is fast in localhost hence giving no time for the AJAX
call to complete.This was the reason I used async:false
.Will it happen (making no AJAX call) if I use async:true
in practical case as well?If yes, is there a way to come around this problem?