有人可以解释为什么 clearTimeout 函数在下面不起作用吗?每当我调用它时,仍然会调用 setTimeout 函数......谢谢!
let todo = document.getElementById('todo');
let todoList = document.getElementById('todo_list');
let timer;
form.addEventListener('submit', newtodo);
function newtodo() {
let todoItem = document.createElement('li');
let timesItem = document.createElement('i');
timesItem.classList.add('fa', 'fa-times-circle');
todoItem.innerHTML = todo.value;
todoList.appendChild(todoItem).prepend(timesItem);
timesItem.addEventListener('click', function() {
todoItem.style.textDecoration = "line-through";
todoItem.style.color = "grey";
timer = setTimeout(removeTodoItem, 3000);;
timesItem.addEventListener('click', function() {
todoItem.style.textDecoration = "none";
todoItem.style.color = "black";
clearTimeout(timer);
});
});
todo.value = '';
function removeTodoItem() {
todoList.removeChild(todoItem);
}
}