Hey. I want to call another method (or condition) for the first time when calling a method, for another call, it will not be called anymore

    3 answers 3

    It is possible so:

    function firstTimeCalled() { alert('call me one time'); } function test() { this.called = this.called || firstTimeCalled() || true; } test(); test(); 

    The first time the test is called, the firstTimeCalled function will be called.

    You can see it here: http://jsfiddle.net/ApGfg/

    • Thank you. I liked the way very much - koza4ok
    • not at all) - Pavel Azanov

    It is fashionable to get an object, and in it a method and a field. The field will be a flag, and when calling the method, we will switch the flag.

      MyObj = { var isRunner = false; // Установим в false, ведь не было ни одного запуска var fFunction = function(){ // Первым делом проверим флаг if(isRunner){ alert("Эта функция уже была вызвана"); return; }else{ // Переключаем флаг this.isRunner = true; // Твой код } }; } 

    Well, call this method

    • By the way, through the flag I did. The neighbor suggested. Thank you. - koza4ok
     var Mtd = { open: function() { this.openFirst(); alert(2); }, openFirst: function() { alert(1); this.open = this.openFirst; } } var t = Mtd.open(); alert('next'); var r2 = Mtd.open(); 
    • Tolkovo. Thank you. - koza4ok