Nikita True
Nikita True

Reputation: 73

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.

Upvotes: 5

Views: 69

Answers (3)

guest271314
guest271314

Reputation: 1

Try using $.Callbacks("once") to call circle at most once if condition ($(this).scrollTop()+h) >= $(".services-procent-ul").offset().top returns true

    var callbacks = $.Callbacks("once");

    callbacks.add(circle);

    var scroller = function() {
          if ($(window).scrollTop() > 0) {
            $('.nav2').fadeIn('slow');
            $('.nav1').fadeOut('fast');
          } else {
            $('.nav2').fadeOut('fast');
            $('.nav1').fadeIn('slow');
          }
       }

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

    });

Upvotes: 2

ecarrizo
ecarrizo

Reputation: 2809

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

Upvotes: 2

antyrat
antyrat

Reputation: 27765

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");

Upvotes: 5

Related Questions