Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could pass the function itself in, for example:</p> <pre><code>function preloadImage(callback) { varLink=".... link to my picture..." $("#picture").one("load", callback).attr("src", varLink); } </code></pre> <p>Then you can call it passing the function, for example</p> <pre><code>function doSomething() { alert("I'm loaded!, my src is: " + this.src); } preloadImage(doSomething); </code></pre> <p>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:</p> <pre><code>//define it function preloadImage(imageUrl, callback) { $("#picture").one("load", callback).attr("src", imageUrl); } //example call preloadImage('myImage.jpg', function () { alert("I'm loaded!, my src is: " + this.src); }); </code></pre> <p>One last thing to keep in mind, you should bind the <code>load</code> event handler <em>before</em> setting the <code>src</code>, to make sure it fires in all cases (from cache for example), just like I have above.</p> <hr> <p>Edit, adding version for comments:</p> <pre><code>function preloadImage(callback) { varLink=".... link to my picture..." $("#picture").one("load", function() { callback.apply(this, arguments); $('#ajax-loader').hide(); }).attr("src", varLink); } </code></pre> <p>There's an alternative as well, keep it simple and bind the "every time" function separately, like this:</p> <pre><code>//this handler happens on all future loads $("#picture").load(function() { $('#ajax-loader').hide(); }); //this handler runs just once for this load function preloadImage(callback) { varLink=".... link to my picture..." $("#picture").one("load", callback).attr("src", varLink); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload