jquery - 如何给ajax加载器超时?
问题描述
我在为加载程序添加计时器时遇到问题,
我希望加载程序花费更长的时间来调用该函数。
我已经$.ajaxSetup
加载了这样的装载机
a.ajaxSetup({
headers: {"X-CSRF-TOKEN": a('meta[name="csrf-token"]').attr("content")},
beforeSend: function(){
a('.page-overlay').addClass("loading");
},
complete: function() {
a('.page-overlay').removeClass("loading");
},
});
然后我这样称呼$.ajaxSetup
:
a(document).on('click', 'a#details_transactions', function(e){
var d = a(this).data('pid'),
s = a('#CallModal'),
w = a('.modal-content');
e.preventDefault(),
a.post(transaction_details,{_token: csrf_token, pid: d}).done((e) => {
s.modal('show');console.log(e);w.html(e)
}).fail((e)=>{s.modal('hide');Toast.fire({icon: 'error',title: 'Something Wrong. Please try again!'})});
});
如果是这样,如何添加计时器以使加载程序看起来需要很长时间才能加载?
已编辑
我尝试setTimeout
在两者之间使用
s.modal('show');console.log(e);w.html(e)
然后
setTimeout(function() {
s.modal('show');console.log(e);w.html(e)
},2000)
但在timeOut
.
我认为有问题,.done
因为这个调用后.done
函数。
那么,我应该使用$.ajax
with success:
for give atimer
吗?
解决方案
所以你有$.ajaxSetup()
它适用于你可能有的所有ajax请求......
但是对于以下特定请求,您希望加载程序被延迟。
我认为您的解决方案将是使用其中的global
参数$.post()
以避免$.ajaxSetup()
触发该参数。
是否为此请求触发全局 Ajax 事件处理程序。参考。
因此,您可以.loading
在这里以不同的方式进行管理,而不会影响其余部分。
a(document).on('click', 'a#details_transactions', function(e){
e.preventDefault();
var d = a(this).data('pid'),
s = a('#CallModal'),
w = a('.modal-content');
a('.page-overlay').addClass("loading"); // add the loading class here
// Notice the global: false here!
a.post({
url: transaction_details,
data: {_token: csrf_token, pid: d},
global: false
}).done((e) => {
// Use the setTimeout here
setTimeout(function(){
s.modal('show');console.log(e);w.html(e);
a('.page-overlay').removeClass("loading"); // remove the loading class here
},2000);
}).fail((e)=>{
s.modal('hide');
a('.page-overlay').removeClass("loading"); // remove the loading class here too!
Toast.fire({icon: 'error',title: 'Something Wrong. Please try again!'})
});
});
推荐阅读
- prometheus-alertmanager - 我正在为普罗米修斯模板化电子邮件通知。有人可以分享一些为 promethius 警报管理器设置的电子邮件模板吗
- oracle - 完整模式下的 Oracle 导入 (impdp) 问题
- python - ModuleNotFoundError:python 3.6 中没有名为“异常”的模块
- javascript - 如何使用 Javascript 更改 api 的 JSON 参数?
- reactjs - “警告:遇到两个用同一个钥匙的孩子”重要吗?
- python - 为 pandas 索引添加一个值
- c# - 将多个文本框文本绑定到单个列表或字符串数组
- linux - 想从一个txt文件复制到另一个
- mysql - 如何使用 PowerShell 脚本将 .sql 文件导入 MySQL?
- swift - For Loop Swift:从不相关的类型转换总是失败