Enable and disable function in jquery

1

I need to disable a function in jquery, for example, when I click on a div, a function that I set will no longer be executed, just when I click on another div it works again. The function loads when the site loads. I need when I click on a div this function does not work anymore, just when you click on another div it comes back.

function executar_whell() {
    function debounce(fn, delay) {
        var timer = null;
        return function () {
            var context = this, args = arguments;
            clearTimeout(timer);
            timer = setTimeout(function () {
                fn.apply(context, args);
            }, delay);
        };
    }
    var scrollervateira = debounce(function (event) {
        if (event.originalEvent.wheelDelta >= 0) {
            $('.flexhome').flexslider('prev');
            console.log('passa_slide');
            return false;
        } else {
            $('.flexhome').flexslider('next');
            console.log('volta_slide');
            return false;
        }
    }, 250);
    $(window).bind('mousewheel', scrollervateira);
}
;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

The run_whell function will not work when I click on a div (div onclick = otherfunno) and when I click on another div (div onclick-work) it will work again.

    
asked by anonymous 12.01.2016 / 17:57

2 answers

2

You can use addEventListener to enable the event and removeEventListener to remove the event.

Follow an example with the mousemove event.

var x = document.getElementById("x");
var y = document.getElementById("y");
var track = document.getElementById("track");

var onMouseMove = function (event) {
  x.value = event.x;
  y.value = event.y;
}

track.addEventListener("click", function (event) {
  var ativo = track.dataset.ativo == "true";
  if (ativo) {
    track.dataset.ativo = false;
    track.value = "Ativar";
    window.removeEventListener("mousemove", onMouseMove);
  } else {
    track.dataset.ativo = true;
    track.value = "Desativar";
    window.addEventListener("mousemove", onMouseMove);
  }
});

window.addEventListener("mousemove", onMouseMove);
<div>
  <label>
    X:
    <input id="x" type="text" />
  </label>
</div>
<div>
  <label>
    Y:
    <input id="y" type="text" />
  </label>
</div>
<div>
  Track Mouse: 
  <input id="track" type="button" value="Desativar" data-ativo="true" />
</div>
    
12.01.2016 / 18:15
1

As a complement to TobyMosque's response, I leave an option with a similar effect, but done in JQuery.

To add an event in jquery you can use .bind() , since to remove this event you can use .unbind() . See the JsFiddle below:

JsFiddle

A very simple code as an example:

$('input:text').bind('keyup', function(){
  $('span').text(this.value);
})
$('input:button').click(funtion(){
  $('input:text').unbind('keyup');
})

With unbind() you can also specify the function in question to remove, along with the event. For example: unbind('keyup', escrever) , so the escrever() function will no longer be executed in the keyup event.

I did not use Stack Snippet because I'm on the mobile.

    
12.01.2016 / 19:03