2016-11-27 59 views
0

我想让用户在块内的HTML更改之前等待2秒钟。你能不能帮我设置了一些时间,使Ajax请求是没有那么快,我有时间显示一些动画我想延迟jQuery ajax成功的功能

我试图设置超时时间,但它不工作

jQuery(document).ready(function($) { 
 

 
$(document).on('click', '#wp-request', function(event){ 
 
\t    event.preventDefault(); \t 
 
      \t var path = myScript.pluginsUrl + '/simple/widget-final.php'; 
 
\t \t \t \t $.ajax({ 
 
\t \t \t \t \t type : 'POST', 
 
             url: path, 
 
             cache: false, 
 
\t \t \t \t \t success : function(data){ 
 
\t \t \t \t \t var newWidget = $(data); 
 
\t \t \t \t \t $('#widget-container').replaceWith(newWidget); 
 
\t \t \t \t \t }, 
 
\t \t \t \t \t error : function(){ alert('Error'); }, 
 
\t \t \t \t \t dataType : 'html' 
 
       }); 
 
\t \t \t \t 
 
}); 
 

 
$(document).on('click', '#wp-back', function(event){ 
 
\t \t \t \t event.preventDefault(); 
 
\t \t \t \t var path = myScript.pluginsUrl + '/simple/widget-initial.php'; 
 
\t \t \t \t $.ajax({ 
 
\t \t \t \t \t type : 'POST', 
 
             url: path, 
 
             cache: false, 
 
\t \t \t \t \t success : function(data){ 
 
\t \t \t \t \t var newWidget = $(data); 
 
\t \t \t \t \t $('#widget-container').replaceWith(newWidget); 
 
\t \t \t \t \t }, 
 
\t \t \t \t \t error : function(){ alert('Error'); }, 
 
\t \t \t \t \t dataType : 'html' 
 
       }); 
 
\t \t \t \t 
 
}); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id="widget-container"> 
 
<p>We are on first page</p> 
 
<a id="wp-request" href="">Send</a> 
 
</div>

+0

在成功回调中应用'setTimeout' –

回答

2

只需在成功回调函数中使用setTimeout即可。

success : function(data){ 
    setTimeout(function(){ 
     var newWidget = $(data); 
     $('#widget-container').replaceWith(newWidget); 
    },2000); // The millis to wait before executing this block 
}, 
+0

哇,它正在工作。谢谢。大 – kilogram