我也太難了吧,求大佬給個代碼參考一下,不勝感激。
uj5u.com熱心網友回復:
ajax請求就是異步的啊uj5u.com熱心網友回復:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<textarea id="result" style="width:80%;height:600px;"></textarea>
<button id="btn1">添加學生資訊</button>
</body>
<script type="text/javascript">
function $(id) {
return document.getElementById(id)
}
const students = []
function showResult() {
var result = JSON.stringify(students, null, 4)
if (!students.length) result = "當前無學生資訊"
$("result").value = result
}
function afterSeconds(delay = 1000) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, delay);
})
}
async function addStudent(student) {
await afterSeconds()
students.push(student)
}
showResult()
$("btn1").addEventListener("click", function () {
addStudent({name: "張三",sex: "男",})
.then(res => {
showResult()
return addStudent({name: "李四",sex: "女",})
})
.then(res => {
showResult()
})
}, false)
</script>
</html>
uj5u.com熱心網友回復:
看下ajax轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/152730.html
標籤:JavaScript
