jQuery延迟使用append() [英] jQuery delay to work with append()

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

问题描述

我无法使jQuery的延迟功能与追加功能一起使用。怎么了?有没有办法让它发挥作用?我想避免直接使用 setTimeout 让客户更容易理解,他们将自己维护,没有任何经验。

I can't make the delay function of jQuery works with append function. What is wrong? Is there a way to make it work? I want to avoid using setTimeout directly to make it more easy to follow for the customer, who will maintain it himself, without any experience.

我的代码:

$('#chatwindow').append('test').delay(2000).append('test');

在此代码中,我同时打印'testtest',

In this code, I get 'testtest' printed at the same time, delay is ignored.

推荐答案

这是因为 delay(2000) 默认排队 fx 队列,其中 append()永远不属于。

This is because delay(2000) queues the fx queue by default, which append() is never part of.

相反,你可以专门排队 append() 使用 queue() 功能。

Instead, you can specifically queue append() on it using the queue() function.

$('#chatwindow').append('test').delay(2000).queue(function (next) {
    $(this).append('test');
    next();
});

您可以在此处看到此示例; http://jsfiddle.net/mj8qC/

You can see an example of this working here; http://jsfiddle.net/mj8qC/

但是,我同意与@ ascii-lime的评论;我希望客户有更多的机会理解 setTimeout ,因为它是JavaScript的基本部分,不像 delay(),混淆许多用户(来自StackOverflow上提出的问题的经验)。

However, I agree with @ascii-lime's comment; I expect the customer will have more chance understanding setTimeout as it's a fundamental part of JavaScript, unlike delay(), which confuses many users (from experience on questions asked on StackOverflow).

FWIW,这个问题引发我写的使用的http://www.mattlunn.me.uk/blog/2012/06/jquery-delay-not-working-for-you/\">博客文章延迟();它比这个答案更详细,可能会为其他人提供更好的阅读。

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆