0

I would like execute a list of functions, something like this:

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

Its possible ???

UPDATE

Well, the point is that for event.keycode I have to execute a lis of functions, but I want set the list of functions of generical type, example:

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

JQueryExtension.KeyBoardControl = function (event) {

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

And at the end make this:

window.onkeypress = JQueryExtension.KeyBoardControl;

:D !!

UPDATE

Ok, some thing like this, might Works ??

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;

UPDATE

Holly God,something like this:

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;
        }
    };

:O !!!

  • Yes, you can put function objects into array and write a code that will execute them sequentially. – PM 77-1 Feb 27 '15 at 03:35
  • possible duplicate of [Javascript Array of Functions](http://stackoverflow.com/questions/4908378/javascript-array-of-functions) – jdphenix Feb 27 '15 at 03:49
  • Well, the point is that for event.keycode I have to execute a lis of functions, but I want set the list of functions of generical type, example: `JQueryExtension.eventKeyCode[33] = [f1(), f2(), fn() ...]; JQueryExtension.KeyBoardControl = function (event) { if (event.shiftKey && event.keyCode == 33) { JQueryExtension.eventKeyCode[event.keyCode]; return; } };` – Freddy Castelblanco Macias Feb 27 '15 at 04:04

4 Answers4

4

Traverse your array of functions within the bound listener function and call each one:

var funcs = [f1, f2, f3, f4];

$(window).on("keypress", function() {
  
  for(var i in funcs) {
    if(!funcs.hasOwnProperty(i)) {
      continue;
    }
    
    funcs[i]();
  }
  
});
Will Reese
  • 2,801
  • 2
  • 15
  • 27
0

You can use something like this:

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

Note: Make sure that the functions are in scope.

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
  • Well, the point is that for event.keycode I have to execute a lis of functions, but I want set the list of functions of generical type, example: `JQueryExtension.eventKeyCode[33] = [f1(), f2(), fn() ...]; JQueryExtension.KeyBoardControl = function (event) { if (event.shiftKey && event.keyCode == 33) { JQueryExtension.eventKeyCode[event.keyCode]; return; } };` – Freddy Castelblanco Macias Feb 27 '15 at 04:02
0

Why not doing like this

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

or like this

$(function(){
  $(window).keypress(function(e){
    f1();
    f2();
    ...
    ...
    ..a list of functions here
  });
});
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40
  • Well, the point is that for event.keycode I have to execute a lis of functions, but I want set the list of functions of generical type, example: `JQueryExtension.eventKeyCode[33] = [f1(), f2(), fn() ...]; JQueryExtension.KeyBoardControl = function (event) { if (event.shiftKey && event.keyCode == 33) { JQueryExtension.eventKeyCode[event.keyCode]; return; } };` – Freddy Castelblanco Macias Feb 27 '15 at 04:03
0

I have solved my question and the full implementation is here::

First I define some variables:

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

The data prepare:

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(); }]
            }
        );
    };

Call the function:

Billing.PreconditionalExecutions();

After I set the window.onkeypress function like this:

window.onkeypress = JQueryExtension.KeyBoardControl;

And the execution is this one:

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;
        }
    };

End !!! :D

Any sugestion about code optimization I will thank