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
谢谢!
$ .delay用于延迟队列中的动画,而不是停止执行。
试试这个:
setTimeout(function() { // Do something after 2 seconds }, 2000);
.delay()
默认只适用于动画函数,你可以使用.promise()
方法:
$("div").html("clicked").delay(2000).promise().done(function() { $(this).html("2 seconds have passed"); });
如果你想在延迟后运行一些代码,请使用setTimeout()
…
$("button").click(function(e) { $("div").html("clicked"); setTimeout(function() { $("div").html("2 seconds have passed"); }, 2000); });
这是你的jsbin的更新版本……