javascript - 我对 JS 中的 setInterval 有什么错误
问题描述
我正在发送 Toast 通知。
我希望当我点击“Toast Notification”按钮时,通知消息会出现在底部,过一会儿,消息就会消失。
所以我将消失时间设置为 5s ,然后单击按钮后 5 秒后,消息依次消失。
但消失的时间,除了第一次消失外,并不是5秒。
时间越来越快,消失不见。这不是我之前设置的 5 秒
我的英语不好,所以我希望你通过运行下面的代码来理解
let toast = document.querySelector('.toast');
let notification = document.querySelector(".notification")
let sec = 5000;
toast.addEventListener('click', () => {
let newDiv = document.createElement("span");
let newText = document.createTextNode("No Way!");
newDiv.classList.add("notification_word");
newDiv.appendChild(newText);
notification.appendChild(newDiv);
if (document.querySelector(".notification_word")) {
setInterval(function() {
removeNotification()
}, sec);
}
})
function removeNotification() {
document.querySelectorAll(".notification_word")[0].remove()
}
body {
text-align: center;
justify-content: center;
align-items: center;
}
button.toast {
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 100px;
font-size: 20px;
background: rgb(215, 188, 240);
margin: -100px 0 0 -100px;
border: 1px solid rgb(28, 3, 51);
border-radius: 20px;
cursor: pointer;
}
button.toast:hover {
background: rgb(178, 157, 201);
}
.notification {
display: flex;
flex-direction: column-reverse;
position: absolute;
right: 2%;
bottom: 2%;
}
span.notification_word {
background-color: rgb(196, 77, 243);
padding: 10px 0;
margin: 3px;
width: 100px;
border: 1px solid rgb(28, 3, 51);
border-radius: 5px;
}
<button class="toast">Toast Notification</button>
<div class="notification">
</div>
解决方案
在您的代码中发生 -> 单击按钮后 5 秒,单击按钮后出现的通知消失。
在我编写的代码中,延迟 5 秒后,它将删除最旧的通知:它将循环
我想你可以理解。
试试这段代码...
let toast = document.querySelector('.toast');
let notification = document.querySelector(".notification");
let sec = 5000;
let Divarray = [];
let arraynum = 0;
toast.addEventListener('click', () => {
let newDiv = document.createElement("span");
let newText = document.createTextNode("No Way!"+arraynum);
newDiv.classList.add("notification_word");
newDiv.appendChild(newText);
notification.appendChild(newDiv);
Divarray[arraynum] = newDiv;
arraynum += 1;
})
function array(){
setInterval(function(){
Divarray.splice(arraynum, 1);
removeNotification();
},sec);
}
array();
function removeNotification() {
document.querySelectorAll(".notification_word")[0].remove();
}
body {
text-align: center;
justify-content: center;
align-items: center;
}
button.toast {
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 100px;
font-size: 20px;
background: rgb(215, 188, 240);
margin: -100px 0 0 -100px;
border: 1px solid rgb(28, 3, 51);
border-radius: 20px;
cursor: pointer;
}
button.toast:hover {
background: rgb(178, 157, 201);
}
.notification {
display: flex;
flex-direction: column-reverse;
position: absolute;
right: 2%;
bottom: 2%;
}
span.notification_word {
background-color: rgb(196, 77, 243);
padding: 10px 0;
margin: 3px;
width: 100px;
border: 1px solid rgb(28, 3, 51);
border-radius: 5px;
}
<button class="toast">Toast Notification</button>
<div class="notification">
</div>
推荐阅读
- php - 如何修复代码在单个数据库中使用 20 个 datalist HTML 标记在 datalist 中显示 1000 行但在 datalist 中仅显示前 512 行?
- java - 使用 Https 运行集成的 Angular 项目和 Spring Boot
- mysql - 更新查询返回操作数应包含 1 列
- scala - java.net.SocketTimeoutException:使用 spark 2.2 将数据帧加载到 teradata 时连接超时
- jquery - jQuery getJSON 到 axios
- asp.net-core - 是否可以在 Windows 10 中检索统一写入筛选器的状态?
- sharepoint - 将 SharePoint 列表项轮流分配给个人列表
- java - Docker java api , java - docker
- python - imagenet 分类错误
- javascript - Javascript查找第N位数字算法性能