-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallbacks.js
More file actions
36 lines (32 loc) · 939 Bytes
/
callbacks.js
File metadata and controls
36 lines (32 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const posts=[
{
title:'Post One',
body:'This is post one',
createdAt:new Date().getTime()
},
{
title:'Post Two',
body:'This is post two',
createdAt:new Date().getTime()
}
];
let intervalId=0;
function getPosts(){
clearInterval(intervalId);
intervalId=setInterval(()=>{
let output='';
posts.forEach((post,index)=>{
output+=`<li>${post.title} - created at ${((new Date().getTime()-post.createdAt)/1000).toFixed(0)} seconds ago</li>`;
});
document.body.innerHTML=output;
console.log('timer id:',intervalId);
},1000);
}
function createPost(post,callback){
setTimeout(()=>{
posts.push({...post, createdAt: new Date().getTime()});
callback();
},2000);
}
createPost({title:'Post Three',body:'This is post three'},getPosts);
createPost({title:'Post Four',body:'This is post four'},getPosts);