jQuery中的wait()或sleep()函数? [英] wait() or sleep() function in jquery?

查看:231
本文介绍了jQuery中的wait()或sleep()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个课程,等待2秒钟,然后添加另一个课程.

I want to add a class, wait 2 seconds and add another class.

.addClass("load").wait(2sec).addClass("done");

有什么办法吗?

推荐答案

setTimeout将在一段时间(以毫秒为单位)的延迟后执行一些代码.但是,重要的一点是:由于javascript的特性,在设置计时器后,其余代码将继续运行:

setTimeout will execute some code after a delay of some period of time (measured in milliseconds). However, an important note: because of the nature of javascript, the rest of the code continues to run after the timer is setup:

$('#someid').addClass("load");

setTimeout(function(){
  $('#someid').addClass("done");
}, 2000);

// Any code here will execute immediately after the 'load' class is added to the element.

这篇关于jQuery中的wait()或sleep()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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