How to reset an event handler after calling .off() in Jquery

余生长醉 提交于 2021-01-29 08:30:29

问题


Why is my second body.on() not working? I added the .off() because if not both mousewheel events triggered with only one mousewheel down event... Isnt the second body.on() supposed to reset the .off()? How should I program this?

$(document).ready(function() {
  $("body").on('mousewheel', function(event, delta) {
    if (event.deltaY < 0) {
      if (!$(".both").hasClass('rotated')) {
        $(".both").css('transform', 'rotate(180deg)');
        setTimeout(function() { $(".both").addClass('rotated') }, 1000);
      } 
    }
    $("body").off();
  });
  $("body").on('mousewheel', function(event, delta) {
    if (event.deltaY < 0) {
      if ($(".both").hasClass('rotated')) {
        alert("a");
      }
    }
  });
});

I add a working solution to my problem in case anyone needs, all thanks to the selected answer

$(document).ready(function() {

  function handleWheel(event) {
    if (event.deltaY < 0) {
      if (!$(".both").hasClass('rotated')) {
        $(".both").css('transform', 'rotate(180deg)');
        setTimeout(function() { $(".both").addClass('rotated') }, 1000);
      }
    }
    // disengage just this event handler and no others
    $("body").off('mousewheel', handleWheel);
  };
  function handleWheelNoche(event) {
    if (event.deltaY < 0) {
      if ($(".both").hasClass('rotated')) {
        setTimeout(function() { $(".black").addClass('noche') }, 1000);
      }
    }
  };
  $("body").on('mousewheel', handleWheel);
  $("body").on('mousewheel', handleWheelNoche);
});

回答1:


Your code registers two mousewheel event handlers on the body object. When the mousewheel event occurs and the event handler callback functions are called, the first event handler then calls $("body").off(); which deregisters BOTH event handlers so you will get no future events.

At that point in time, you no longer have any event handlers on the body object.

If you want an event handler to only be called once, then you can use .one(). Other than that, it is not clear why you have two separate event handlers so therefore it's not clear what else to advise.

Generally, there's no reason to have two separate event handlers for the same event. Whatever work you want to do, you can just carry out that work in one single event handler. If you only want to carry out some of the work during some events, then just implement logic inside that single event handler to decide which work to carry out any time the event handler is called (using if statements and the like).

If you only want to deregister one of the event handlers, then you have to use a named function with the event handler $('body').on('mousewheel', funcName); so you can call $('body').off('mousewheel', funcName) to deregister just that particular event handler.


Using a named function works like this:

$(document).ready(function() {

  function handleWheel(event) {
    if (event.deltaY < 0) {
      if (!$(".both").hasClass('rotated')) {
        $(".both").css('transform', 'rotate(180deg)');
        setTimeout(function() { $(".both").addClass('rotated') }, 1000);
      } 
    }
    // disengage just this event handler and no others
    $("body").off('mousewheel', handleWheel);
  }

  $("body").on('mousewheel', handleWheel);

});


来源:https://stackoverflow.com/questions/23070437/how-to-reset-an-event-handler-after-calling-off-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!