执行函数列表jquery

时间:2015-02-27 03:32:51

标签: javascript jquery

我想执行一个函数列表,如下所示:

window.onkeypress = [f1(), f2(), f3(), fn() ...] ...

它可能???

更新

嗯,关键是对于event.keycode我必须执行lis函数,但是我想设置一般类型的函数列表,例如:

JQueryExtension.eventKeyCode[33] = [f1(), f2(), fn() ...];

JQueryExtension.KeyBoardControl = function (event) {

    if (event.shiftKey && event.keyCode == 33) {
        JQueryExtension.eventKeyCode[event.keyCode];
        return;
    }
};`

最后这样做:

window.onkeypress = JQueryExtension.KeyBoardControl;

:D !!

更新

好的,像这样的东西,可能会有效吗?

JQueryExtension.eventKeyCode = new Array();

JQueryExtension.KeyBoardControl = function (event) {

        if (event.shiftKey && event.keyCode == 33) {

            for (var i = 0; i < JQueryExtension.eventKeyCode.length; i++) {
                if (JQueryExtension.eventKeyCode[i] == event.keyCode) {
                    var listOfFunctions = JQueryExtension.eventKeyCode[i];
                    for (var j = 0; j < listOfFunctions.length; j++) {
                        listOfFunctions[j]();
                    }
                }
            }

            return;
        }
    };
window.onkeypress = JQueryExtension.KeyBoardControl;

更新

霍莉上帝,这样的事情:

var functionsForKeyCodeOne = [
            {
                keyCode: 33,
                theFunction: Billing.TheKnockOutDataTableViewModelCustomerSearcher.mainData([])
            },
            {
                keyCode: 33,
                theFunction: Billing.SetModalVisibility("searcherCustomerModal", "show")
            },
            {
                keyCode: 33,
                theFunction: $('#searcherCustomerModal').on('shown.bs.modal', function() {
                    $("#txtNameCustomer").focus();
                })
            }
        ];

        JQueryExtension.eventKeyCode.push(functionsForKeyCodeOne);

window.onkeypress = JQueryExtension.KeyBoardControl;

JQueryExtension.KeyBoardControl = function (event) {

        if (event.shiftKey && event.keyCode == 33) {

            for (var i = 0; i < JQueryExtension.eventKeyCode.length; i++) {
                if (JQueryExtension.eventKeyCode[i].keyCode == event.keyCode) {
                    var listOfFunctions = JQueryExtension.eventKeyCode[i];
                    for (var j = 0; j < listOfFunctions.length; j++) {
                        listOfFunctions[j].theFunction();
                    }
                }
            }

            return;
        }
    };

:哦!!!

4 个答案:

答案 0 :(得分:4)

遍历绑定侦听器函数中的函数数组并调用每个函数:

&#13;
&#13;
var funcs = [f1, f2, f3, f4];

$(window).on("keypress", function() {
  
  for(var i in funcs) {
    if(!funcs.hasOwnProperty(i)) {
      continue;
    }
    
    funcs[i]();
  }
  
});
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用以下内容:

window.onkeypress = function() {
    f1();
    f2();
    f3();
    .
    .
    .
};

注意: 确保功能在范围内。

答案 2 :(得分:0)

为什么不这样做

$(function(){
  $('your_selector').on('keypress', function(){
    f1();
    f2();
    ...
    ...
    ..a list of functions here
  });
});

或者像这样

$(function(){
  $(window).keypress(function(e){
    f1();
    f2();
    ...
    ...
    ..a list of functions here
  });
});

答案 3 :(得分:0)

我已经解决了我的问题,完整的实现就在这里::

首先我定义一些变量:

JQueryExtension.eventKeyCode = new Array(); //The Functions List of keycode
    JQueryExtension.availableKeyCode = new Array(); // The key code that I want evaluate
    JQueryExtension.notExistElementOnArray = -1; // For indexOf if not exists on the combination that I require

数据准备:

Billing.PreconditionalExecutions = function() {

        for (var i = 33; i <= 40; i++) {
            JQueryExtension.availableKeyCode.push(i);
        }

        JQueryExtension.eventKeyCode.push({
                keyCode: 33,
                theFunctionList: [
                    function() {
                        Billing.TheKnockOutDataTableViewModelCustomerSearcher.mainData([]);
                    },
                    function() {
                        BootStrapExtension.SetModalVisibility("searcherCustomerModal", "show");
                    },
                    function() {
                        BootStrapExtension.OnShowModal("searcherCustomerModal",
                        [
                            function() {
                                JQueryExtension.SetFocusedElement("txtNameCustomer");
                            }
                        ]);
                    }
                ]
            },
            {
                keyCode: 34,
                theFunctionList: [function() { BootStrapExtension.SetModalVisibility("searcherProductModal", "show"); }]
            },
            {
                keyCode: 35,
                theFunctionList: [function() { theMainViewModel.newBillingCommand(); }]
            },
            {
                keyCode: 36,
                theFunctionList: [function() {}]
            },
            {
                keyCode: 37,
                theFunctionList: [function() { theMainViewModel.finishBillingCommand(); }]
            },
            {
                keyCode: 38,
                theFunctionList: [function() { theMainViewModel.holdCommand(); }]
            },
            {
                keyCode: 39,
                theFunctionList: [function() { theMainViewModel.abortCommand(); }]
            },
            {
                keyCode: 40,
                theFunctionList: [function() { theMainViewModel.canceledCommand(); }]
            }
        );
    };

调用该函数:

Billing.PreconditionalExecutions();

我设置window.onkeypress函数之后:

window.onkeypress = JQueryExtension.KeyBoardControl;

执行就是这个:

JQueryExtension.KeyBoardControl = function (event) {

        if (event.shiftKey && (JQueryExtension.availableKeyCode.indexOf(event.keyCode) != JQueryExtension.notExistElementOnArray)) {

            for (var i = 0; i < JQueryExtension.eventKeyCode.length; i++) {
                if (JQueryExtension.eventKeyCode[i].keyCode == event.keyCode) {
                    var listOfFunctions = JQueryExtension.eventKeyCode[i];
                    for (var j = 0; j < listOfFunctions.theFunctionList.length; j++) {
                        listOfFunctions.theFunctionList[j]();
                    }
                    break;
                }
            }

            return;
        }
    };

结束!!! :d

任何有关代码优化的消息我都会感谢