如何在 AJAX 函数中添加延迟函数

how to add delay function inside the ajax function

本文关键字:函数 延迟 添加 AJAX      更新时间:2023-09-26

我正在使用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);
    }  
});