当前位置: 首页 > news >正文

一款基于jQuery Ajax的等待效果

特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过。如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处: http://www.cnblogs.com/mao2080/

1、效果示例

1、加载中效果

2、加载后效果

2、代码样例

 1 var ajaxUtil = {
 2             /**为保证load图标不会一闪而过,小于600毫秒的请求将延时加载*/
 3             loadShowTime:600,
 4             /**
 5              * Ajax请求
 6              * @param {Object} url 请求的url
 7              * @param {Object} params 参数(json类型,如:{userName:'admin', email:'mao2080@sina.com'})
 8              * @param {Object} successCallBack 自定义函数-成功时返回
 9              * @param {Object} errorCallBack 自定义函数-失败时返回
10              * @param {Object} args 其他参数{"loadingId":null}
11              */
12             ajaxRequest : function(url, params, successCallBack, errorCallBack, args){
13                 args = ajaxUtil.showLoading(args);
14                 $.ajax({
15                     url:url,
16                     data:params,
17                     type:"get",
18                     dataType:"json",
19                     async:true,
20                     success:function(res){
21                         if(res.success || res.code == 200){
22                             args.timestamp = new Date().getTime()-args.timestamp;
23                             if(args.timestamp || args.timestamp > ajaxUtil.loadShowTime){
24                                 window.setTimeout(function(){
25                                     ajaxUtil.hideLoading(args);
26                                     successCallBack(res);
27                                 }, ajaxUtil.loadShowTime-args.timestamp);
28                             }else{
29                                 ajaxUtil.hideLoading(args);
30                                 successCallBack(res);
31                             }
32                         }else{
33                             ajaxUtil.hideLoading(args, true);
34                             if(errorCallBack){
35                                 errorCallBack(res);
36                             }
37                         }
38                     },
39                     error:function(res){
40                         ajaxUtil.hideLoading(args);
41                         alert("请求失败...");
42                     },
43                 });
44             },
45             /**
46              * 显示加载loading
47              * @param {Object} args
48              */
49             showLoading:function(args){
50                 args = !args?{}:args;
51                 args.timestamp = new Date().getTime();
52                 if(args.loadingId){
53                     var container = $(args.loadingId);
54                     if(container){
55                         container.css({"position":"relative"});
56                         container.append('<div class="loading" style="width:60px; height:24px; position:absolute;left:50%;top:50%;margin-left:-30px;margin-top:-12px;"><img src="img/loading-0.gif"/></div>');
57                     }
58                 }
59                 return args;
60             },
61             /**
62              * 隐藏加载loading
63              * @param {Object} args
64              */
65             hideLoading:function(args){
66                 if(args.loadingId){
67                     var container = $(args.loadingId);
68                     if(container){
69                         container.find('.loading').remove();
70                     }
71                 }
72             }
73         }
74         
75         $(function(){
76             ajaxUtil.ajaxRequest("data.json", null, function(res){
77                 //处理请求成功
78                 $("#userName").html(res.data.userName);
79                 $("#email").html(res.data.email);
80             }, function(res){
81                 //处理请求失败
82             }, {loadingId:"#test1"})
83         });

3、资料下载

load-demo.rar

转载于:https://www.cnblogs.com/mao2080/p/6952594.html

相关文章:

  • uTorrent 被发现悄悄安装挖矿程序,BitTorrent 公司否认
  • TortoiseGit为github账号添加SSH keys,解决pull总是提示输入密码的问题
  • Npcap —— 基于 Winpcap/ Libpcap 的网络包抓取库
  • Lucene 个人领悟 (二)
  • Java线程挂起
  • DOM2和DOM3
  • noip 2008 双栈排序
  • 双向绑定底层剖析(用于自定义组件双向数据绑定和非form表单元素)
  • 利用阿里云 OSS 搭建私有 Docker 仓库
  • cocos2d-x 3.0的坑有哪些
  • 第十节:委托和事件(2)(泛型委托、Func和Action、事件及与委托的比较)
  • read 系统调用剖析【转】
  • vue2.0-transition配合animate.css
  • LintCode 带最小值操作的栈
  • 第七 其它模块及面向对象
  • 网络传输文件的问题
  • [iOS]Core Data浅析一 -- 启用Core Data
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • httpie使用详解
  • isset在php5.6-和php7.0+的一些差异
  • Javascript编码规范
  • js作用域和this的理解
  • Leetcode 27 Remove Element
  • MySQL Access denied for user 'root'@'localhost' 解决方法
  • Mysql5.6主从复制
  • Spark RDD学习: aggregate函数
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • text-decoration与color属性
  • vue-router的history模式发布配置
  • windows下如何用phpstorm同步测试服务器
  • 翻译--Thinking in React
  • 分享自己折腾多时的一套 vue 组件 --we-vue
  • 构建工具 - 收藏集 - 掘金
  • 近期前端发展计划
  • 让你成为前端,后端或全栈开发程序员的进阶指南,一门学到老的技术
  • 如何进阶一名有竞争力的程序员?
  • 如何在 Tornado 中实现 Middleware
  • 三分钟教你同步 Visual Studio Code 设置
  • 用element的upload组件实现多图片上传和压缩
  • 怎么把视频里的音乐提取出来
  • postgresql行列转换函数
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • 大数据全解:定义、价值及挑战
  • ​iOS实时查看App运行日志
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #控制台大学课堂点名问题_课堂随机点名
  • (2)Java 简介
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (附源码)springboot太原学院贫困生申请管理系统 毕业设计 101517
  • (论文阅读笔记)Network planning with deep reinforcement learning
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (转)jdk与jre的区别
  • (转)人的集合论——移山之道
  • ./configure、make、make install 命令
  • .bat批处理(十一):替换字符串中包含百分号%的子串