0

I have a link that has a method attached to the onclick event. I am unable to edit the existing html or javascript method but I am able to add additional javascript.

I need to fire some jquery after the fixedMethod() method completes.

So for example in the sample below I need to make the output 1, 2 instead of 2, 1 without changing the HTML of the link or fixedMethod() but the rest of the javascript I can change.

This is a contrived example, in my actual use case I don't know how long the method will take to complete.

function fixedMethod(){
  setTimeout(function(){console.log(1)}, 2000);
}

$("#someLink").click(function(){
  console.log(2);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<a id="someLink" href="#" onclick="fixedMethod()">click here</a>

Is this possible?

Guerrilla
  • 13,375
  • 31
  • 109
  • 210
  • What sort of method? If it's asynchronous and returns a Promise, call `then` on it? If it's not asynchronous, just call the second function right below? – CertainPerformance Jun 01 '18 at 06:33
  • @LGSon because I have no way to know how long fixedMethod() will take to complete – Guerrilla Jun 01 '18 at 06:34
  • `in my actual use case I don't know how long the method will take to complete` nope, unless that function somehow can indicate its completion – Jaromanda X Jun 01 '18 at 06:35
  • @JaromandaX Thanks for confirming. – Guerrilla Jun 01 '18 at 06:37
  • This might be an option: https://stackoverflow.com/questions/11222618/append-code-to-the-end-of-an-existing-function – Asons Jun 01 '18 at 06:38
  • Unfortunately the method is not in same scope as where I can add my JS (different doc ready blocks) – Guerrilla Jun 01 '18 at 06:44
  • If the method change the DOM, you can use [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) – Asons Jun 01 '18 at 06:47
  • Ahh I think that will work! The script modifies one of two locations depending on outcome so I can monitor them for changes :) – Guerrilla Jun 01 '18 at 06:53

0 Answers0