Question

I have function a:

$(document).ready(function a(){
        $('.c, .d').click(function() { 

            $("f").each(function(i) {
                c=i+1
                print c
    });
});
});

and I've function b:

$(document).ready(function b(){
       $("f").each(function(i) {
                c=i+1
                print c      
    });
});

I want to detect if the function a is clicked and triggered. then disable the function b , how to do that in javascript?

Was it helpful?

Solution

Add a declaration of disable outside the function scopes: var disable = false;, set this variable to true in function a() and check it in function b(). Like so:

a.js:

var disable = disable || false;

$(document).ready(function () {
    function a() {
        disable = true;

        $('.c, .d').click(function () { 
            $('f').each(function (i) {
                c = i + 1;
                // print c
            });
        });
    }
});

b.js:

var disable = disable || false;

$(document).ready(function () {
    function b() {
        if (typeof disable !== 'undefined' && disable)
            return;

        $('f').each(function (i) {
            c = i + 1;
            // print c      
        });
    }
});

OTHER TIPS

If function a is called, add a line which stores some value in a form that indicates a was called:

(document).ready(function a(){
     document.forms.FunctionAForm.inputname.value = 'yes';
     $('.c, .d').click(function() { 

            $("f").each(function(i) {
                c=i+1
                print c
        });
    });
});

The modify function b as follows:

$(document).ready(function b(){
   if (document.forms.FunctionAForm.inputname.value === 'yes') {
       return false;
   } else {
       $("f").each(function(i) {
            c=i+1
            print c      
       });
   }
 });

The function does not execute since function a was already called, which has been stored in the form.

You can take one flag like active and make it true in your function A and check if it is true or not in function b like below

$(document).ready() {
var active = false;

function a(){
                active = true;
                $('.c, .d').click(function() { 

            $("f").each(function(i) {
                c=i+1
                print c      
              });
           }

function b(){
           if(!active)
           {
                 return;
           }
           $("f").each(function(i) {
                c=i+1
                print c      
           });
}
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top