Open lgwebdream opened 4 years ago
111
<html>
<head>
<script src="./index.js" defer></script>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<button>update</button>
<ul></ul>
</body>
</html>
// 初始化待办列表
const todoList = Array(1000)
.fill(undefined)
.map((e, i) => {
return {
id: i,
text: `todo-${i}`,
done: false,
};
});
// 随机将指定数量的代办变为过期
function randomDone(number) {
const todoCount = todoList.filter((e) => !e.done).length;
if (!number || !todoCount) {
return;
}
const randomId = Math.floor(Math.random() * 1000);
if (todoList[randomId].done) {
randomDone(number);
} else {
todoList[randomId].done = true;
randomDone(number - 1);
}
}
// 渲染1000个代办
function renderTodoList() {
const ul = document.querySelector("ul");
const frag = document.createDocumentFragment();
todoList.forEach((item) => {
const li = document.createElement("li");
li.textContent = `${item.text}${item.done ? "-已过期" : ""}`;
frag.append(li);
});
// 先清空容器
ul.innerHTML = ""
ul.append(frag);
}
renderTodoList();
const btn = document.querySelector("button");
btn.addEventListener("click", () => {
randomDone(100);
renderTodoList();
});
<template>
<ul>
<li v-for="item in todoList" :key="item.id">{{ item.text }}{{ item.done ? "-已过期" : "" }}</li>
</ul>
</template>
<script>
export default{
data(){
return {
todoList:[]
}
},
}