开发者

using .each to execute array of functions on .resize

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开发者_StackOverflow中文版.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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜