medk
medk

Reputation: 9549

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.

Upvotes: 0

Views: 576

Answers (2)

MrRap
MrRap

Reputation: 137

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.

Upvotes: 1

jli
jli

Reputation: 6623

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

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

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

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

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

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

This seems trivial, so I'm not sure I understand the question completely.

Upvotes: 1

Related Questions