0

I need to do 2 things on click. The first happens straight away, then the second thing happens after the first thing has finished.

I'm trying to chain functions as callbacks, but this doesn't seem to be working.

$(document).ready(function(){
  $('button').click(function(){
    // first thing
    console.log('click');
  }, function(){
    // second thing
    console.log('callback');
  });
});

That code only logs the second message. I need it to do the first, then the second. What's the correct way of doing this?

3
  • 1
    Can't you call a function inside first handler for the second action? Commented Feb 9, 2015 at 12:55
  • Try to use mousedown and mouseup events, so you can do it like: $('button').mousedown(function(){console.log('FIRST');}).mouseup(function(){console.log('SECOND');}); Commented Feb 9, 2015 at 12:55
  • What is "first thing", can you post the code? Commented Feb 9, 2015 at 12:56

2 Answers 2

3

You could try something like this... calling the second function at the conclusion of the first:

$(document).ready(function () {
    $('button').click(function () {
        one();
    });
});

function one() {
    // first thing
    console.log('click');
    two();
}

function two() {
    // second thing
    console.log('callback');
}

DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

Simply add the second function inside the first; in the case below I've automatically called it too.

$(document).ready(function(){
  $('button').click(function(){
    // first thing
    console.log('click');
    (function(){
       // second thing
       console.log('callback');
     })();
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.