ajax 同步模式與異步模式
阿新 • • 發佈:2019-01-19
scrip 布爾值 sync 區別 ole get () rip 布爾
<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>
ajax 同步模式與異步模式