Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a click event handler for particular time?

Only JavaScript, No jquery.

Code goes like:

window.onload = addListeners;
function addListeners(){
    for(var  i = 0 ; i < document.getElementsByClassName('arrow').length; i++){
    if(window.addEventListener){
        document.getElementsByClassName('arrow') [i].addEventListener( 'click', func , false);

    }else{
        document.getElementById('arrow') [i].attachEvent('onclick' , func);
    }
}
}


function func(){
//Takes exactly 5 seconds to execute
}

Now, I want to disable the 'click' for 5 seconds when the function 'func()' is running. And, then after the 'func()' is completely executed, the click should again be enabled automatically.

How to do this only using JavaScript?

like image 450
Navneet Saini Avatar asked Jun 03 '13 12:06

Navneet Saini


People also ask

How do I disable on click event?

To disable the click event in HTML, the “pointer-events” property of CSS is used. For this purpose, add an HTML element and set the value of the pointer-events property as “none” to disable its click event. This article explained how to disable the click event using CSS along with its example.

How do you make Eventlistener work only once?

Using the once option We can pass an object as an argument to the addEventListener method and specify that the event is only handled once. This is achieved by passing the property once to the object. If we set once to true, the event will only be fired once.

How do I trigger event on disabled button?

If an element is disabled, it looks as "onclick" events are not fired. The sample of the code: <input id="subm_tc" class="btn btn-primary" type="submit" disabled="" value="Log in" name="Submit"> $("#subm_tc"). click(function () { if($("#modlgn-tc").is(':checked')){ alert('checked'); } else { alert('unchecked'); } });


2 Answers

Rather than disable the click event, check a variable to see if its currently running.

var funcRunning = false;
function func(){
  if (funcRunning) return;
  funcRunning = true;

  //Function logic here

  funcRunning = false;
  return //whatever  
}

This way your not guessing the function will take 5 seconds to run, the function will simply not execute its logic until its completed its current run.

EDIT: As @NULL has suggested, a better method would be to store the boolean variable on the function itself to prevent global pollution:

function func(){
  if (func.IsRunning) return;
  func.IsRunning = true;

  //Function logic here

  func.IsRunning = false;
  return //whatever  
}
like image 86
Curtis Avatar answered Oct 05 '22 23:10

Curtis


To elaborate my comments on Curts answer:

When declaring the function func you can do two things:

function func() {
    if ( !func.isRunning ) {
        func.isRunning = true;
        /* logic goes here */
        func.isRunning = false;
    }
}

Or you can make a closure and store isRunning inside it:

var func = (function() {
    var isRunning = false;
    return function() {
        if ( !isRunning ) {
            isRunning = true;
            /* logic goes here */
            isRunning = false;
        }
    };
})();

The second example can makes a private variable only accessible inside the closure.


Its mostly about a design pattern some developers doesn't like to store variables directly on functions like in example one. The only difference is that if someone chooses to set func.isRunning = true the function cannot run again and therefore not reset itself.

like image 22
Andreas Louv Avatar answered Oct 06 '22 00:10

Andreas Louv