Leo Loki
Leo Loki

Reputation: 109

Jquery: Why is delay not working?

I added a delay but it is not working...

<div id="sum"></div>рублей​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
$('#sum').html('3000').delay(3000).html('5000');

http://jsfiddle.net/Jv8g6/

Why is delay not working?

Upvotes: 2

Views: 93

Answers (2)

Fr&#233;d&#233;ric Hamidi
Fr&#233;d&#233;ric Hamidi

Reputation: 263167

delay() only affects the animation queue, and html() does not use that queue.

However, you can call queue() and invoke html() from its callback function to achieve the effect you want:

$("#sum").html("3000").delay(3000).queue(function() {
    $(this).html("5000").dequeue();
});

You will find an updated fiddle here.

Upvotes: 3

Rory McCrossan
Rory McCrossan

Reputation: 337733

delay() applies to actions stored in the queue - such as animations. html() does not use the queue, therefore the code above does not behave as you expect.

For non-queued actions, you will need to use setTimeout() to delay execution. Try this:

$('#sum').html('3000');
setTimeout(function() {
    $('#sum').html('5000');
}, 3000);

Upvotes: 2

Related Questions