-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
63 lines (49 loc) · 1.95 KB
/
script.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function createDomElements(todos){
var parentElement = document.getElementById("mainArea");
var currentChildren = Array.from(parentElement.children);
console.log(currentChildren);
let added = 0, deleted=0, updated=0;
todos.forEach(function (item){
var existingChild = currentChildren.find(function (child){
return child.dataset.id === String(item.id);
})
if(existingChild){
updated++;
existingChild.children[0].innerHTML = item.title;
existingChild.children[1].innerHTML = item.description;
currentChildren = currentChildren.filter(function (child){
return child != existingChild;
});
} else {
added++;
var childElement = document.createElement("div");
childElement.dataset.id = item.id;
var grandChildElement1 = document.createElement("span");
grandChildElement1.innerHTML = item.title;
var grandChildElement2 = document.createElement("span");
grandChildElement2.innerHTML = item.description;
var grandChildElement3 = document.createElement("button");
grandChildElement3.innerHTML = "Delete";
grandChildElement3.setAttribute("onclick","deleteTodo("+ todos[i].id + ")")
childElement.appendChild(grandChildElement1);
childElement.appendChild(grandChildElement2);
childElement.appendChild(grandChildElement3);
parentElement.appendChild(childElement);
}
})
currentChildren.forEach(function (child){
deleted++;
parentElement.removeChild(child);
} )
};
window.setInterval(()=>{
let todos = [];
for( let i = 0; i< Math.floor(Math.random() *100); i++){
todos.push({
title: "Go to Gym",
description: "Some description",
id: i+1
})
}
createDomElements(todos)
}, 3000)