Tuấn Nguyễn
Tuấn Nguyễn

Reputation: 1

How to use jquery resize run funtion one time?

I want to use run 2 functions jquery when resize window. But I just want to do they one time. code can be like:

jQuery(document).ready(function () {
    var screenwidth=jQuery(window).width();
    if(screenwidth>991){
       dofunction1();
    }
    else{
      dofunction2();
    }
});

I want when screenwidth>991 do dofunction1() 1 time, and when screenwidth<=991 do dofunction2() 1 time. Hope your help!!

Upvotes: 0

Views: 60

Answers (1)

Satpal
Satpal

Reputation: 133403

You can use .one() to bind resize event

Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

$(window).one('resize', function () {
    var screenwidth = jQuery(window).width();
    if (screenwidth > 991) {
        dofunction1();
    } else {
        dofunction2();
    }
});

Upvotes: 1

Related Questions