jQuery .delay does not delay

自作多情 提交于 2019-12-02 11:41:22

$.delay is used to delay animations in a queue, not halt execution.

Try this:

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

.delay() by default only works with animating functions, you can use .promise() method:

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

Use setTimeout() if you want to run some code after a delay...

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!