1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to prevent a function executes after being executed

Discussion in 'JavaScript' started by ketting00, Feb 3, 2014.

  1. #1
    Hi,
    Is there anyway to stop function c executes again after it is being executed by function a.
    
    var a = function() {
       c(1);
    }
    var b = function() {
       c(2);
    }
    var c = function(i) {
       var d = 3 + i;
       console.log(d);
    }
    a();
    b();
    
    Code (markup):
    Thank you,
     
    Solved! View solution.
    ketting00, Feb 3, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Javascript supports function overloading; more so since you made it a classed variable.

    So undefine it in 'a'.

    c = null;
     
    deathshadow, Feb 4, 2014 IP
  3. #3
    Well I'm not necessarily sure it's a great idea to completely overwrite c. He merely wants to prevent it from executing in b is what I'm gathering.

    A basic example using the existing code would be to set a Boolean variable specifying whether or not c can be executed.

    
    var executeC = true;
    var a = function() {
       c(1);
       executeC = false;
    }
    var b = function() {
       if (executeC) {
          c(2);
       }
    }
    var c = function(i) {
       var d = 3 + i;
       console.log(d);
    }
    a();
    b();
    
    Code (markup):
     
    Iconiplex, Feb 10, 2014 IP
    ThePHPMaster likes this.
  4. Stephen Veit

    Stephen Veit Member

    Messages:
    4
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    33
    #4
    Instead of setting it to null, I would set it to a null function. That way it wouldn't error if it is called again:

    
    var a = function() {
       c(1);
       c = function () {};
    }
    
    Code (markup):
     
    Stephen Veit, Feb 11, 2014 IP
  5. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #5
    Your best bet is to do it this way :-
    var a = function (){
        c(1,true);
    }
    var b = function (){
        c(2,false);//or simply c(2);
    }
    var c = function (i,execute){
        if(!execute)return;
        var d = 3 + i;
        console.log(d);
    }
    a();
    b();
    Code (markup):
     
    spaceman12, Feb 20, 2014 IP
  6. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #6
    Spaceman12 your code is wrong. In your example if you only call b(); without a(); beforehand the code in C will never execute even though it is intended to do so.

    I like Iconiplex implementation the best.
     
    stephan2307, Feb 21, 2014 IP