我正在尝试创建一个数组,用于保存出现在DOM上的所有待处理错误消息(使用jquery实现),然后循环遍历该数组以查看是否存在任何错误消息需要调用,如果有,则在执行后将其删除。
我的问题是我无法想出如何将函数推入数组并执行。以下是我目前的代码:
我会给您展示一个完整的
我的问题是我无法想出如何将函数推入数组并执行。以下是我目前的代码:
var dialogQueue = []
dialogQueue.push(errorCall("test", "test", "test", "test"));
for (var queueNum = 1, 1 < dialogQueue.length, 1++) {
alert(dialogQueue[1])
}
如果有帮助的话,以下是显示错误消息的代码:
function dialogShow() {
$(".dialog-con").css("display", "block").css("background", "rgba(0,0,0,.8)")
$(".body-wrapper").addClass("errorFilter");
$(".dialog-anim").animate({
opacity: 1,
marginTop: "-=20px"
})
setTimeout(function () {
$(".errorFilter").addClass("blur");
}, 100);
}
function dialogHide() {
$(".dialog-con").css("background", "rgba(0,0,0,.0")
$(".body-wrapper").removeClass("blur");
$(".dialog-anim").animate({
opacity: 0,
marginTop: "-=25px"
}, 300)
setTimeout(function () {
$(".dialog-con").css("display", "none");
$(".body-wrapper").removeClass("errorFilter");
// Code for removing the function from the array after pushing OK on the dialog
}, 1000);
}
function errorCall(title, sub, text, code) {
$(".dialog .title").text(title);
$(".dialog .subtitle").text(sub);
$(".dialog .text").html(text);
$(".dialog .error-code").html(code);
dialogShow();
}
我会给您展示一个完整的
errorCall()
函数示例:
代码如下:
function dialogShow() {
$(".dialog-con").css("display", "block").css("background", "rgba(0,0,0,.8)")
$(".body-wrapper").addClass("errorFilter");
$(".dialog-anim").animate({
opacity: 1,
marginTop: "-=20px"
})
setTimeout(function () {
$(".errorFilter").addClass("blur");
}, 100);
}
function dialogHide() {
$(".dialog-con").css("background", "rgba(0,0,0,.0")
$(".body-wrapper").removeClass("blur");
$(".dialog-anim").animate({
opacity: 0,
marginTop: "-=25px"
}, 300)
setTimeout(function () {
$(".dialog-con").css("display", "none");
$(".body-wrapper").removeClass("errorFilter");
}, 1000);
}
function errorCall(title, sub, text, code) {
$(".dialog .title").text(title);
$(".dialog .subtitle").text(sub);
$(".dialog .text").html(text);
$(".dialog .error-code").html(code);
dialogShow();
}
errorCall("Hello stackoverflow!","This is how my error message dialog looks!","Blah blah blah blah","Code code code");
.dialog-con {
height: 100%;
display: none;
position: fixed;
width: 100%;
background-color: rgba(0, 0, 0, .0);
z-index: 50;
transition: ease 300ms;
}
.dialog-anim {
width: 100%;
height: 100%;
display: none;
display: flex;
flex-direction: column;
justify-content: center;
opacity: 0;
margin-top: -20px;
}
.dialog {
margin: auto;
padding: 12px 27px;
background: white;
border-radius: 3px;
width: 520px;
transform: translateY(30px)
}
.dialog .title-con {
margin-bottom: 25px;
}
.dialog .title {
font-size: 35px;
padding-bottom: 7px;
}
.dialog .error-code {
margin-top: 15px;
color: rgba(0, 0, 0, .6);
font-family: "Courier New", Courier, monospace
}
.dialog .subtitle {
font-size: 17px;
color: rgba(0, 0, 0, 0.4);
}
.dialog .text {}
.dialog .button-con {
margin-top: 25px;
}
.dialog button {
margin: auto;
float: right;
color: white;
border: none;
padding: 9px 37px;
background: #10b5ff;
text-transform: uppercase;
font-weight: bold;
border-radius: 3px;
}
.dialog button:hover {
background: black;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dialog-con">
<div class="dialog-anim">
<div class="dialog">
<div class="title-con">
<div class="title">Error Message Title</div>
<div class="subtitle"></div>
</div>
<div class="text-con">
<div class="text">Error Message</div>
<div class="error-code"></div>
</div>
<div class="button-con" onclick="dialogHide()">
<button>Ok</button>
</div>
</div>
</div>
</div>
(确定按钮位移是由于视口太小造成的,请忽略它。)
因此,我想这样做的原因是,如果某些事情触发了多个错误,它们会被推送到数组中并逐一显示(按下“确定”按钮会显示下一个错误等等)。
errorCall
将其结果添加到一个数组中。这样,无论在何处或如何调用它,它都将始终使用该数组。 - Malk