0

Note: This is an asynchronous version of this question I asked yesterday: What happens when an event is triggered and page update both take place at the same time?

Given I have HTML like this:

<a id="google" href="http://google.com">Google.com</a>

And Javascript like this:

$('#google').on('click', function (event) {
  event.preventDefault();
  asyncFuncThatLogsAnalytics(this);
  // Do some more
  console.log("Click Click Bang Bang!");
  window.location.href = $(this).attr('href'); 
});

When I click my Google link, the click event fires and the page immediately loads the next link…

What can I do to ensure all events within the click event have fired/finished BEFORE changing the window's location?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3084728
  • 565
  • 1
  • 8
  • 16
  • if asyncFuncThatLogsAnalytics() method returns a promise interface, then use done() or always() method. If not, then make it returns it. I'm not sure google analytics expose such kind of method but a callback should be available, somewhere. Basically, what you want is there: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – A. Wolff Jan 29 '14 at 18:01
  • Do you have any examples of how the code for a `promise` interface would look? – user3084728 Jan 29 '14 at 18:38

0 Answers0