Wolfgang Adamec
Wolfgang Adamec

Reputation: 8524

jQuery deferreds

I'm new to jquery deferreds. Here I have a simple example.

Can anybody tell me why the done function ("now it's my time") is fired before the other functions is done?

The guys in this example also create a deferred object and return a promise, so do I.
How must I change my little example to get the done function only fired after this 6 seconds (after the timeout)?

Thanks alot in advance
Wolfgang

Upvotes: 2

Views: 153

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262939

You should pass a function to the done() method, but instead you're calling console.log() immediately and passing its return value to done(). You should write:

$.when(test()).done(function() {
    console.log("now it's my time");
});

Instead of:

$.when(test()).done(console.log("now it's my time"));

You will find an updated fiddle here.

Upvotes: 6

Related Questions