【问题标题】:JQuery delay() function being skipped跳过 JQuery delay() 函数
【发布时间】:2012-03-30 18:54:31
【问题描述】:

我无法让一些 javascript 和 Jquery 延迟适当的时间。我想更改一些文本,等待 5 秒,然后弹出警报。

代码如下:

$('#result').html("Record has passed").delay(5000);
alert("Record has passed");

由于某种原因,警报在 jquery 更改#result 并等待之前运行。任何解决方案或其他人看到这个问题?

我试过了

setTimeout($('#result').html("Record has passed"), 5000);

也一样,但仍然没有运气。

【问题讨论】:

  • 我还应该补充一点,我也尝试过移动 .delay(5000):$('#result').delay(5000).html("Record has passed");

标签: jquery delay wait


【解决方案1】:

jQuery 的.delay 方法仅适用于动画和队列函数。尝试改用setTimeout

$("#result").html("Record has passed");
setTimeout(function(){
    alert("Record has passed");
},5000);

【讨论】:

    【解决方案2】:

    如果你还想使用 jquery 延迟,你可以这样做:

    $('#result').html("Record has passed").delay(5000).queue(function() {
      alert("Record has passed");
    });
    

    【讨论】:

      【解决方案3】:
      $('#result').html("Record has passed");
      setTimeout(function(){
          alert("Record has passed");
      },5000);​
      

      Example Here.

      【讨论】:

        猜你喜欢
        • 2011-03-29
        • 2011-12-13
        • 2018-07-27
        • 1970-01-01
        • 2011-06-08
        • 1970-01-01
        • 1970-01-01
        • 2021-10-03
        • 2019-08-01
        相关资源
        最近更新 更多