using .each to execute array of functions on .resize

浪尽此生 提交于 2020-01-06 04:30:09

问题


In the an object called Response this works to trigger a function on .ready and .resize simultaneously...

Response.action = function ( func ) {
    if ( typeof func !== 'function' ) { return false; } // If func is not a function, return false.
        $(function () { func(); $(window).resize( func ); }); // 
    return func;
}; // Response.action

...using this to call it:

Response.action( myfunc );
function myfunc() { 
    //do stuff
}

(We worked that out in this thread.)

I'd like to make another version that can do the same thing for an array of functions, with usage like this:

Response.actionSet( [myfunc1, myfunc2] );
function myfunc1() { 
    //do stuff
}
function myfunc2() { 
    //do stuff
}

I tried it as below and every other incantation I could imagine, but I haven't got it to work. No error messages either. Can anyone recommend how to get this working:

Response.actionSet = function ( arr ) {
        if ( arr.isArray !== true ) { return false; } // If arr is not an array, return false.
        $.each(arr, Response.action(this)); // iterate over arr array
        return arr;
}; // Response.actionSet

回答1:


You have a small error; it should be $.each(arr, function() { Response.action(this); });

Response.actionSet = function(arr) {
    if(!$.isArray(arr)) return false;
    $.each(arr, function() { Response.action(this); });
    return arr;
};


来源:https://stackoverflow.com/questions/7375158/using-each-to-execute-array-of-functions-on-resize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!