Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable after its execution

I have 2 events

First:

$(window).on("scroll", function() {
    if (($(this).scrollTop()+h) >= $(".services-procent-ul").offset().top){
        circle();
        $(window).off("scroll");
    }
});

Second:

$(window).on("scroll", function() {
if($(window).scrollTop() > 0){
  $('.nav2').fadeIn('slow');
  $('.nav1').fadeOut('fast');
}else{
  $('.nav2').fadeOut('fast');
  $('.nav1').fadeIn('slow');
}
});

The first event I need to disable after its execution. But I need to work a second event.

like image 855
Nikita True Avatar asked Oct 07 '15 14:10

Nikita True


2 Answers

You can add event namespace to first event and disable it then by namespace wherever you want:

$(window).on("scroll.once", function() {
    ...
    $(window).off("scroll.once");
like image 181
antyrat Avatar answered Oct 06 '22 04:10

antyrat


If you want to "disable" an event after its execution You could use Jquery .one()

The .one() method is identical to .on(), except that the handler is unbound after its first invocation

$(window).one("scroll", function() {
   //doSomething
});

If you are looking for a way to disable certain event callback after certain condition and not disable all of them You can add Event Namespace as @antyrat answered

like image 40
ecarrizo Avatar answered Oct 06 '22 04:10

ecarrizo