Noth Wild Noth Wild - 2 months ago 9
jQuery Question

jQuery: Compactly function window.resize (doesn't repeat)

I have this code:

$(function someFunction() {
//here is my function
});

$(window).on('resize', function() {
//here the same
});


Is there a better way to rewrite this more compactly?

Answer

Sure, any number of ways

$(function() {
    $(window).on('resize', function() {
       // here is your function
    }).trigger('resize');
});

or

function doStuff() {
    // here is your function
}

$(doStuff);
$(window).on('resize', doStuff);
Comments