ajax 同步模式与异步模式

<script>

// console.time('abc')
// for (var i = 0; i < 100000000; i++) {}
// console.timeEnd('abc')

// console.log('begin request')
var xhrAsync = new XMLHttpRequest();
// open 方法的第三个参数是 async 可以传入一个布尔值,默认为 true
xhrAsync.open('GET', 'time.php', true);
console.time('async');
xhrAsync.send();
console.log(xhrAsync.responseText);
// console.log('end request')
console.timeEnd('async');


// 同步模式 ajax 操作会有楞等的情况
// 区别在于 send 方法会不会出现等待情况
// console.log('begin request')
var xhrSync = new XMLHttpRequest();
// open 方法的第三个参数是 async 可以传入一个布尔值,默认为 true
xhrSync.open('GET', 'time.php', false);
console.time('sync');
xhrSync.send();
console.log(xhrSync.responseText);
// console.log('end request')
console.timeEnd('sync')

</script>
原文地址:https://www.cnblogs.com/lujieting/p/10291267.html