javascript - 如何在ajax函数中添加延迟函数

标签 javascript jquery

我正在使用ajax函数来更新和输出结果。 如何在第二次更新结果再次显示之前延迟 2 秒? 因为现在,在第一次更新和第二次更新之后,文本是一样的 “已更新”我想让用户知道这是第二次更新消息。

$.ajax({  
  type: "post",  
  url: "function.php",  
  data: "ajax=updateAward&" + inputs,
  success: function(html) { 

    $('#message_award').html(''); //clear previous text
//delay 2 sec then display below?

    $('#message_award').html(html) ;    

  }  
});

最佳答案

使用setTimeout在延迟(以毫秒为单位)后执行函数。这是一个完整的示例:

$.ajax({  
    type: "post",  
    url: "function.php",  
    data: "ajax=updateAward&" + inputs,
    success: function(html) { 
        setTimeout(function() {
            $('#message_award').html(html); 
        }, 2000);
    }  
});

关于javascript - 如何在ajax函数中添加延迟函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19942636/

相关文章:

Javascript - 使相同的函数具有不同的输入

javascript - getServerSideProps 函数响应无法在 Next.js 中序列化为 JSON

jquery - 如何使用 jquery 切换基于 css 的表列和嵌套内容?

javascript - 如何克隆元素及其关联的事件回调?

javascript - jQuery - 暂时禁用功能然后再次启用它

Javascript 对象文字 : why can't I do this?

javascript - JQuery/dirty forms/window.onbeforeunload 仅在链接选择后触发

javascript - 如何将 Node.js 异步流回调转换为异步生成器?

javascript - Yeoman - 如何将快速服务器任务添加到由生成器 Angular 生成的 Gruntfile.js

jQuery 选择 "not this and not everything in this"