我的代码中有以下功能。这是一个简化版本:
$.ajax({
url: $form.attr('action'),
dataType: 'json',
type: 'POST',
data: $form.serializeArray(),
success: function (json, textStatus, XMLHttpRequest) {
// delay here
alert("hello");
}
有什么方法可以引入延迟。例如,上述代码延迟了5秒?
请注意,如果可能的话,我需要在这里替换//延迟这些词语。
答案 0 :(得分:6)
您可以将setTimeout()
与匿名函数结合使用:
setTimeout(function() { /* your code here */ }, 5000);
这将在5秒延迟后运行该函数内的代码。
答案 1 :(得分:3)
当然可以。
$.ajax({
url: $form.attr('action'),
dataType: 'json',
type: 'POST',
data: $form.serializeArray(),
success: function (json, textStatus, XMLHttpRequest) {
// delay here
setTimeout(function() { alert("hello"); }, 5000);
});
答案 2 :(得分:2)
您将使用setTimeout()
,这就是您将其引入自己的代码的方式:
$.ajax({
url: $form.attr('action'),
dataType: 'json',
type: 'POST',
data: $form.serializeArray(),
success: function (json, textStatus, XMLHttpRequest) {
// save 'this' pointer in case you need it in the setTimeout() function
// because it will be set to something different in the setTimeout() callback
var self = this;
setTimeout(function() {
// execute whatever code you want here with a 5 second delay
alert("hello");
}, 5000) ;
}
答案 3 :(得分:1)
您可以使用setTimeout
方法
如果您想延迟提醒,请使用
success: function (json, textStatus, XMLHttpRequest) {
// delay here
setTimeout(function(){alert("hello");}, 5000); // 5000 is in milliseconds
}