javascript - 将函数推送到数组,循环执行并在执行后删除

时间:2015-07-10 23:58:10

标签: javascript jquery arrays

我正在尝试创建一个数组,其中包含显示在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()功能的小提琴:

&#13;
&#13;
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");
&#13;
.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;
}
&#13;
<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>
&#13;
&#13;
&#13;

(ok按钮位移是微小视口的结果,忽略它。)

所以我想这样做的原因是,如果某些事情触发了多个错误,它们会被推送到数组并逐个显示(按OK显示下一个等)。

3 个答案:

答案 0 :(得分:2)

您需要创建一个函数包装器来将它们存储在数组中。当你将它推送到数组时,你正在调用errorCall。请尝试使用此代码:

var dialogQueue = []

dialogQueue.push(
    function () {
        errorCall("test", "test", "test", "test")
    }
);

for (var queueNum = 0, 1 < dialogQueue.length, queueNum++) {
    alert( dialogQueue[queueNum]() );
}

你也想在执行后删除,所以可以这样做:

while(dialogQueue.length > 0) {
    alert( dialogueQueue[0]() );
    dialogueQueue.shift();
}

这是一个简单的例子:

var funcArr = [];

funcArr.push( console.log("Cat") );
// This immediately calls console.log, logging "cat".  After console.log is
// evaluated we push its return value `undefined`

// Instead, we wrap the console.log in an anonymous function.  This gives us
// a function which will execute what we desire when it is called.
funcArr.push( function() { console.log("cat"); } );

// there is nothing to invoke now, because we are creating a new function.
// now if we:
console.log( funcArr );
// we get: [function anonymous()]

// So if we say:
funcArr[0];
// this will evaluate to:
function() {
    console.log("cat");
};

// Therefore invoking funcArr[0] calls an anonymous function, which runs
// the function we actually wanted to run.
funArr[0]();

答案 1 :(得分:1)

ChadF的方法的另一种方法是在你想要显示消息时实例化一个函数并在其上调用一个方法。

&#13;
&#13;
// Your base function
function error(a, b, c, d) { 
  this.show = function() {
    alert(a + " " + b + " " + c + " " + d);
  };
}

var dialogQueue = [];

// Creating an instance of "error"
dialogQueue.push(new error("test", "test2", "test3", "test4")); 
dialogQueue.push(new error("testing again", "test2", "test3", "test4")); 

alert("Data finished pushing");

for (var i = 0; i < dialogQueue.length; i++) {
    // Calling the "show" method from "error"
    dialogQueue[i].show();
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你可以将args推入数组,并使用它来执行该函数。你可以使用apply用一组参数调用给定的函数。

像这样:

dialogQueue=[];
//fill the queue with arguments
dialogQueue.push([1,2,3,4]);
dialogQueue.push(["test","test","test","test"]);
dialogQueue.push(["bla","bla","bla","bla"]);

//fire off everything in the queue
var len=dialogQueue.length;
for (i=0;i<len;i++) {
//pop off the args, and use apply to call them
var args=dialogQueue.pop();
errorCall.apply(this, args);
}