[英]Synchronous XMLHttpRequest on the main thread is deprecated within embedded Javascript code
I have scripted a simple Ajax function and embedded this to my website.我编写了一个简单的 Ajax function 脚本并将其嵌入到我的网站中。
On the console I get this warning在控制台上我收到此警告
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
不推荐使用主线程上的同步 XMLHttpRequest,因为它对最终用户的体验产生不利影响。
What does this mean and how to avoid it?这是什么意思以及如何避免它?
function readTextFile(file) {
var rawFile = new XMLHttpRequest();
var allText = "";
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
allText = rawFile.responseText;
}
}
}
rawFile.send(null);
return allText;
}
function load() {
var allText = readTextFile('drinks.json');
var mydata = JSON.parse(allText);
var div = document.getElementById('cocktaillist');
div.innerHTML = "";
for (var i = 0; i < mydata.length; i++) {
div.innerHTML = div.innerHTML + "<p class='inner' id="+i+">"+ mydata[i].name +"</p>" + "<br>";
}
}
The message you observed:您观察到的消息:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.
不推荐使用主线程上的同步 XMLHttpRequest,因为它对最终用户的体验产生不利影响。
Is a warning about the fact that a synchronous request defeats the asynchronous nature of JavaScript and blocks the user interface, which causes bad user experience, especially in case of bad network quality.是一个警告,即同步请求会破坏 JavaScript 的异步特性并阻塞用户界面,这会导致用户体验不佳,尤其是在网络质量不佳的情况下。
You should set true
as third parameter of rawFile.open()
and you already provided a rawFile.onreadystatechange
callback in order to execute code when you have received a response, so the rest of your code is fine.您应该将
true
设置为rawFile.open()
的第三个参数,并且您已经提供了rawFile.onreadystatechange
回调以便在收到响应时执行代码,因此您的代码的 rest 很好。
You need to change your code to use asynchronous Ajax requests.您需要更改代码以使用异步 Ajax 请求。 As the warning indicates, using synchronous requests is bad for the user experience.
正如警告所示,使用同步请求不利于用户体验。 Also, there literally never is a reason to use synchronous Ajax requests, ever.
此外,从字面上看,永远没有理由使用同步 Ajax 请求。
And that means you need to use callback functions.这意味着您需要使用回调函数。 Compare:
相比:
function getText(path, success) {
var req = new XMLHttpRequest();
req.open("GET", path, true);
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status === 200) {
success(req.responseText); // call a function with the received value
} else {
console.log("unexpected response status " + req.status);
}
}
};
req.send(null);
}
function load() {
getText('drinks.json', function (allText) { // pass a function to call when the request is done
var mydata = JSON.parse(allText);
var div = document.getElementById('cocktaillist');
div.innerHTML = "";
for (var i = 0; i < mydata.length; i++) {
div.innerHTML = div.innerHTML + "<p class='inner' id="+i+">"+ mydata[i].name +"</p>" + "<br>";
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.