Temporarily disable all events

Let's say I have this piece of javascript code:

$('a').on('mouseenter', function() {console.log('you just hovered a link')});

      

And sometimes I want to disable all events because the page is loading for example. I can achieve this through:

$(*).off();

      

However, how can I re-fire all events on page load?

+3


source to share


3 answers


you can bind an event when the document is ready:

 $(function(){
   $('a').on('mouseenter', function() {console.log('you just hovered a link')});
 });

      



and when it comes to other conditions like when animation is running. you can set some variable (say isanimationruning) to true and check the state of the variable to determine if hover should be used or not:

$('a').on('mouseenter', function() {
 if(!isanimationruning)
   console.log('you just hovered a link')
});

      

+3


source


Try using the detach function if you can. See an example of the detach () function



+1


source


http://jsfiddle.net/washington_guedes/rtonjcym/14/

$("button").on("click", function(){
    if( $(this).val() === "0")
        $(this).html("Click here to disable").val("1");
    else
        $(this).html("Click here to enable").val("0");
});

$("#action").on("click", function(){
    if( $("button").val() === "0") return;

    // function implementation
    $(this).css({"background-color": "#" 
        + Math.floor(Math.random() * 10)
        + Math.floor(Math.random() * 10)
        + Math.floor(Math.random() * 10)});

});

      

I have stored in the value of the button: zero or one ... and used this code in the following functions:

.. function .. (){
  if( $("button").val() === "0") return;
  ...
} ..

      

0


source







All Articles