In my code, I have an array of function calls. I iterate over these calls and use .apply () to call them. The problem is that if calling a new function takes some time, the loop will be .apply () and will call the next function before the previous function is finished. > & L .; Here is an example:
function someFunc(element, calls){ if(calls.length){ fn = calls[0]; calls.shift(); fn.apply(element, args); someFunc(element, calls); } }
So, if there was a callback to the apply function, this could work the way I want it. i.e.
function someFunc(element, calls){ if(calls.length){ fn = calls[0]; calls.shift(); fn.apply(element, args, function(){ someFunc(element, calls); }); } }
I also have a question about calling someFunc inside the callback function. The functions in my calls array affect my element variable. So I want to make sure that after it changes, it will be passed to someFunc in the callback so that the next function can also manipulate it. Sometimes they just confuse me with this context. :)
If this helps, I use jQuery. I know how to add callbacks to jQuery methods, but I donโt know how to do this when I deal with my own JavaScript code. How to add a .apply () method callback?
source share