<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>ES5 回调函数</title>
</head>
<body>
<script>
const posts = [
{title:"Post one", body:"this is post one"},
{title:"Post two", body:"this is post two"}
];
function getPosts () {
setTimeout(() => {
let output = '';
posts.forEach((post,index) =>{
output += `<li>${post.title}</li>`;
});
document.body.innerHTML = output;
},2000)
}
getPosts();
function createPost (post,callback) {
setTimeout(() => {
posts.push(post);
callback();
console.log(posts);
},3000);
}
createPost({title:"Post three",body:"this is post three"},getPosts);
</script>
</body>
</html>