2.1 同步與非同步
什麼是 AJAX?
AJAX = Asynchronous JavaScript and XML
是一種非同步技術(Asynchronous)。早期在寫網頁的時候,尚不支援 AJAX 時,無法做到頁面局部更新資料這件事情,每次點擊連結,都一定是整個頁面重新整理。然而在有了 AJAX 技術之後,使得頁面局部更新內容,得以實現。
瞭解同步、非同步
同步(synchronous):
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
console.log("1");
for(let i = 0; i < 100; i++){
console.log("2");
}
console.log("3");
</script>
</body>
</html>
非同步(asynchronous)程式碼:
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script>
console.log("1");
setTimeout(function(){
console.log("2");
}, 1000);
console.log("3");
</script>
</body>
</html>
Last updated