user3263967
user3263967

Reputation: 25

disable function using javascript

I can not seem to find the code to disable a javascript function. What I want to do is have a javascript function and then I want to disable it. Here is the code:

 <script>
 var fooFunc = function fooFunction() {
      alert("HELLO");
 };

 $(document).ready(function() {
      fooFunc.disable();    
 });
 </script>

 <button onclick="fooFunc()">Button</button>

Basically, when the button is click the function should not work, it should be disabled. Thanks

Upvotes: 0

Views: 2989

Answers (5)

user663031
user663031

Reputation:

"Disabling" fooFunc is the same as setting it to an empty function (not to null--that will cause an error when it is called the next time). In this case:

$(document).ready(function() {
  fooFunc = function() { };
});

But I don't see how this is different from simply removing the onclick handler from the HTML element.

If you want the ability to disable/re-enable the function, you can write it like this:

fooFunc = function() {

  function _fooFunc() {
    if (!enabled) return;
    alert("HELLO");
  }

  var enabled = true;
  _fooFunc.enable  = function() { enabled = true; };
  _fooFunc.disable = function() { enabled = false; };

  return _fooFunc;

}();

If you want to extend this to allow any function to be enabled/disabled, you can write a higher-order function, which takes any function as a parameter, and returns a function with enable and disable methods attached to it:

function disablable(fn) {

  function inner() {
    if (!enabled) return;
    fn();
  }

  var enabled = true;

  inner.enable  = function() { enabled = true; };
  inner.disable = function() { enabled = false; };

  return inner;

}

Now you can define fooFunc as

var fooFunc = disablable(function fooFunction() {
  alert("HELLO");
});

and the rest of your code will work as you want.

Upvotes: 2

Cynthia Vesser
Cynthia Vesser

Reputation: 11

If you don't want the function to work at all and be totally disabled then use the below.

If you want the function to work only under certain conditions then you will need if/else statements so it will work only when the conditions that you have set are met.

$(document).ready(function(){
    $("button").onclick(function(event){
        event.preventDefault();
    });
});

Upvotes: 1

mkkhedawat
mkkhedawat

Reputation: 1717

You Should be doing this :) Change function definition on very first run and you are good to go.

<! DOCTYPE html>
<html lang="en">


<body>
<script>
    var fooFunc = function() {
        alert("HELLO");
        fooFunc = function(){};
    };

    var enablefooFunc = function()
    {
        fooFunc = function() {
            alert("HELLO");
            fooFunc = function(){};
        };
    }

</script>

<button onclick="fooFunc()">Run once and Disable FooFunc</button>
<button onclick="enablefooFunc()">Enable FooFunc</button>
</body>

</html>

Upvotes: 0

David
David

Reputation: 5937

You can access the onclick property of the element..

 <button id="id" onclick="fooFunc()">Button</button>


 <script>
   document.querySelector('#id').onclick = '';
 </script>

Upvotes: 1

nicael
nicael

Reputation: 18995

You were going to define it back to undefined or null.

fooFunc=undefined;

Upvotes: 0

Related Questions