简体   繁体   中英

Why does click event register immediately here?

I am looking to animate something away and then click it. However, when I do the following it clicks the item immediately. Why is that? And how can I remedy that?

$('input[type="submit"]').hide('slow').show('slow').click();

Use the callback functionality.

$('input[type="submit"]').hide( "slow", function() {
  // Do whatever you want in the callback function, this will fire after the hide function finished.
  $(this).click();
});

If you want synchronous code to happen asynchronously as part of the animation queue, the best way is to use the .queue() method:

$('input[type="submit"]')
    .hide('slow')
    .show('slow')
    .queue(function (next) {
        $(this).click();
        next();
    });

Promises will work only so long as you don't queue more animations before the currently executing code has finished.

Complete callbacks are ok, so long as you don't need to perform more animations after. Otherwise, complete callbacks end up looking like arrow code:

$(...).hide('slow', function () {
    $(this).show('slow', function () {
        $(this).click();
    });
});

.queue() helps to flatten arrow code .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM