Chrome Extensions - Asynchronous XHR?
I'm looking for any way to sending asynchrono开发者_StackOverflowus requests via XHR:
http://code.google.com/chrome/extensions/xhr.html
Actually I'm trying to send requests just after page is loaded:
$(document).ready(function() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://myserver.com/", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// Get logged user
username = $("div.user", xhr.responseText).text()
.replace(/.*\(([a-z0-9]*)\).*/, "$1");
}
};
});
But the user experience is really not good, it freezes until it gets data from myserver.com and just after that it displays the popup window.
Is there any way to first display the popup window and after that show some loading text/image to user and AFTER that start pushing requests?
If you are using jquery already then why not use convenient jquery ajax wrapper:
$(document).ready(function() {
getData();
});
function getData() {
$.ajax({
url: "https://myserver.com/",
type: "GET",
dataType: "text",
success: function(data) {
username = $("div.user", data.replace(/.*\(([a-z0-9]*)\).*/, "$1");
}
});
}
Put that script not inside <head>
but at the end of a <body>
. If it still freezes a page then try using setTimeout
:
$(document).ready(function() {
setTimeout(getData, 0);
});
精彩评论