Javascript 限制警报数量和时间的方法有哪些?

Javascript 限制警报数量和时间的方法有哪些?,javascript,html,alert,Javascript,Html,Alert,当我点击“Todo Ekleyin”按钮时,我得到一个警告。但是,我希望此警报每次只显示一次,而不是多次,并且可以在警报消失后再次按下。我如何才能做到这一点,以及? 提前感谢您的回答,干得好。(如果有一种方法不是你建议的方法,我很高兴你能写下它的名字。) //Tüm Elementleri Seçme const form=document.querySelector(“todo form”); const todoInput=document.querySelector(“#todo”);

当我点击“Todo Ekleyin”按钮时,我得到一个警告。但是,我希望此警报每次只显示一次,而不是多次,并且可以在警报消失后再次按下。我如何才能做到这一点,以及? 提前感谢您的回答,干得好。(如果有一种方法不是你建议的方法,我很高兴你能写下它的名字。)

//Tüm Elementleri Seçme
const form=document.querySelector(“todo form”);
const todoInput=document.querySelector(“#todo”);
const todoList=document.querySelector(“.list组”);
const firstCardBody=document.queryselectoral(“.cardbody”)[0];
const secondCardBody=document.queryselectoral(“.cardbody”)[1];
const filter=document.querySelector(“过滤器”);
const clearButton=document.querySelector(“清除todos”);
eventListeners();
函数eventListeners(){//Tüm Event Listenerlar
表格。附录列表(“提交”,附录);
}
函数addTodo(e){
const newTodo=todoInput.value.trim();
如果(newTodo==“”){//Alarm Verme
showAlert(“危险”,“吕特芬-比尔-托多女孩化”);
}
否则{
addtodotui(newTodo);
}
addtodotui(newTodo);
e、 预防默认值();
}
功能showAlert(类型、消息){
常量警报=document.createElement(“div”);
alert.className=`alert-alert-${type}`;
alert.textContent=消息;
firstCardBody.appendChild(警报);
//设置超时
setTimeout(函数(){
alert.remove();
}, 1000);
}
函数addtodotui(newTodo){//String Değerini列表项olarak Ekleyecek。
//清单项目Oluşturma。
const listItem=document.createElement(“li”);
//链接Oluşturma
const link=document.createElement(“a”);
link.href=“#”;
link.className=“删除项目”;
link.innerHTML=“”;
listItem.className=“列表组项目d-flex验证内容之间”;
//文本节点
appendChild(document.createTextNode(newTodo));
appendChild(链接);
//待办事项列表“e列表项”ıEkleme
todoList.appendChild(列表项);
//埃克勒姆·索拉斯·坦·亚兹尔姆
todoInput.value=“”;
}
//Todo Ekleme Bilgi Mesajı

事項清單
事項清單
托多·艾克莱因


托多拉


您可以在警报函数中放入一个整数,并且每次使用数组都会增加一个整数。 例如,如果您希望在5次之后不显示警报

var a=0;
var b=真;
如果(newTodo==“”| | b){//
showAlert(“危险”,“请告诉我该怎么办!”);
a++;
如果(a==5){
b=假;
}

}
您可以在警报函数中放入一个整数,并且每次使用数组都会增加一个整数。 例如,如果您希望在5次之后不显示警报

var a=0;
var b=真;
如果(newTodo==“”| | b){//
showAlert(“危险”,“请告诉我该怎么办!”);
a++;
如果(a==5){
b=假;
}

}
谢谢,伊尔凯,但它不起作用。也许我把它放错地方了。谢谢,伊尔凯,但它不起作用。也许我放错地方了。