Search code examples
jquerycallbackjquery-callback

jQuery callback event


I'm developing a simple captcha system that uses jquery to refresh the image if not readable.

$('a#reload').click(function(){

    $(this).removeClass('reload').addClass('reloading');

    var ts = Math.round((new Date()).getTime() / 1000);

    $('div#captcha-image img').attr('src', 'captcha/captcha.php?t=' + ts);

    $('input#captcha').focus();

});

This code does everything except resetting the reload button to its initial state.

I want to make this function after all the above, like a callback function in ajax.

$(this).removeClass('reloading').addClass('reload');

This function resets the reload button to its initial normal state, so how to call it after everything passes.

Thanks.


Solution

  • You would need to add:

     $('div#captcha-image img').load(function(){
         $(a#reload).removeClass('reloading').addClass('reload');
     });
    

    So if your image is a valid image and is loaded then .load() will wait until it's done to call the callback and change the link.