I have a function
function preloadImage() {
varLink=".... link to my picture..."
$("#picture").attr("src", varLink).one("load", function() {
// This is called after the image is loaded
}}
Now I call this function many times in my jquery script. But I need it to call different functions after the image is loaded. Is there any more elegant way to do that instead of sending parametrs to function like:
function preloadImage(action)
and making several if then statements after the function is executed to replace the: // This is called after the image is loaded in my code?
So I basically need to do several things after image is loaded (callback) so I have to call this function with several different parameters or is there any other way?
Thank you
Jerry
You could pass the function itself in, for example:
Then you can call it passing the function, for example
You may want to pass the image link as well, I’m unclear from your example, but it seems likely you would want to, here’s an example of that, with an anonymous function:
One last thing to keep in mind, you should bind the
loadevent handler before setting thesrc, to make sure it fires in all cases (from cache for example), just like I have above.Edit, adding version for comments:
There’s an alternative as well, keep it simple and bind the “every time” function separately, like this: