I have the following functions that is called every 2 seconds to load some data. It registers the function [do] to do the stuff with the response. (the example is simplified).
function doRequest (){
$.ajax({ url: 'www.google.com.pe', success: function (response) {do(response)} });
}
function do (text){
var i = setInterval(doRequest, 2000);
}
I wonder if there is any way that I can create a function that is called every time the [do] function is called with out needing to add a call to the listener inside the do function. If there is any better way to do it with jQuery, like a plugin I’d appreciate the help.
[Edit] The idea is not whether it works or not. My question was about if I can add a custom listener to the "do" function which was already implemented. Something like addActionListener("do", "after", doSomeThingElse),sSo I could do some thing else just after the do function has finished.
If you want to keep existing code as it is, you could wrap
do()in another function which in turn callsdo()and your new function (saydo_this_as_well()).See the example below (I renamed
do()todo_this()to avoid confusion around the reserved keyworddo). This works because global functions are nothing but variables with function objects in them. These variables can be overwritten, in this case with a new function that calls the old one: