jQuery延迟

扫码查看
本文介绍了jQuery延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jquery递延的新手.这里有一个简单的示例.

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?

示例中的人也创建了一个延迟对象并返回了诺言,我也是.
如何更改我的小示例,以使完成的功能仅在这6秒钟(超时后)之后才被激发?

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

推荐答案

您应该将函数传递给 done()方法,但是您要立即调用console.log()并将其返回值传递给done().您应该写:

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");
});

代替:

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

您将在此处找到更新的小提琴.

You will find an updated fiddle here.

这篇关于jQuery延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 16:56
查看更多