user838531
user838531

Reputation: 488

JavaScript callback functions not executed

I just started learning about callback functions. Unfortunately I can't make this altered sample-code work.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready( function() {
    $(".closebtn").click(function(){
        function1(someVariable, function() {
          function2(someOtherVariable);
        });
    });


    function function1(param, callback) {
        alert("Erste Funktion");
        callback();
    }

    function function2(param) {
        alert("Zweite Funktion");
    }
})

</script>

When I click on the button nothing happens. Can anyone help?

Upvotes: 0

Views: 38

Answers (1)

Rico Kahler
Rico Kahler

Reputation: 19302

Your example works for me. Let me know what you think:

// these need to be defined
var someVariable = 'example value';
var someOtherVariable = 'example value';

$(document).ready(function() {
  $(".closebtn").click(function() {
    function1(someVariable, function() {
      function2(someOtherVariable);
    });
  });


  function function1(param, callback) {
    console.log("Erste Funktion");
    callback();
  }

  function function2(param) {
    console.log("Zweite Funktion");
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="closebtn">Close</button>

Upvotes: 1

Related Questions