jQuery .delay不会延迟

时间:2013-09-24 14:52:37

标签: jquery delay

如何设置元素的html,等待2秒,然后将html设置为其他内容?

示例:$("div").html("clicked").delay(2000).html("2 seconds have passed");

会发生什么:div从蝙蝠那里“2秒钟过去”,而不是说“点击”2秒,然后显示“2秒已经过去”。

我是否需要执行.delay(2000, function() { $("div").html("2 seconds have passed"); })

之类的操作

此处的实例:http://jsbin.com/UfaYusU/1/edit

谢谢!

3 个答案:

答案 0 :(得分:3)

$ .delay用于延迟队列中的动画,而不是停止执行。

试试这个:

setTimeout(function() {
      // Do something after 2 seconds
}, 2000);

答案 1 :(得分:2)

默认情况下,

.delay()仅适用于动画功能,您可以使用.promise()方法:

$("div").html("clicked").delay(2000).promise().done(function() {
    $(this).html("2 seconds have passed");
});

答案 2 :(得分:0)

如果您想在延迟后运行一些代码,请使用setTimeout()

$("button").click(function(e) {
  $("div").html("clicked");
  setTimeout(function() {
    $("div").html("2 seconds have passed");
  }, 2000);
});

Here's an updated version of your jsbin...

相关问题