Dean Jason
Dean Jason

Reputation: 105

off() in jquery to temporary off event

        $(document).off("click", "#main").on("click", "#main", function () {
            that = $(this);
            $('loading').show();
            Delay = setInterval(function () {
                getMenu(that.attr("menu_id"));
            }, 1000);


        });

How can I temporary switch off the event event for #main? because if not #main is clickable during the loading, and caused my function to execute multiple times.

Upvotes: 0

Views: 148

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You can use a class to make it off like

$(document).off("click.main").on("click.main", "#main:not(.disabled)", function () {
    var that = $(this);
    $('loading').show();
    Delay = setInterval(function () {
        getMenu(that.attr("menu_id"));
    }, 1000);
});

here if the #main has a class called disabled the click handler will not get executed.

So when you want to disable click add the class disabled and when you want to enable it back remove the class

Upvotes: 1

Related Questions