码迷,mamicode.com
首页 > Web开发 > 详细

防止ajax重复提交

时间:2017-09-13 19:16:15      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:timeout   延迟   urlencode   else   ram   $.ajax   data   post   com   

var $Common = {
request: { //请求 ajaxTimeFn: { time: null, url: null, data: null }, //防止重复提交 延迟 ajax: function (url, data, callback, async) { /// <summary> /// ajax 请求 /// </summary> /// <param name="url">服务器地址</param> /// <param name="data">请求参数</param> /// <param name="callback">回调函数</param> /// <param name="async">是否同步 默认:false关闭异步 true或不设置 均为开启异步</param> /// <param name="isLoading">是否运行Loading...(运行loading会出现无法同步运行问题)</param> //防止多次提交 if ($Common.request.ajaxTimeFn.url == url && $Common.request.ajaxTimeFn.data == data) { $Common.request.ajaxTimeFn.time = setTimeout(function () { action(function () { clearTimeout($Common.request.ajaxTimeFn.time); $Common.request.ajaxTimeFn.time = null; }); }, 400); } else { action(function () { clearTimeout($Common.request.ajaxTimeFn.time); $Common.request.ajaxTimeFn.time = null; }); } $Common.request.ajaxTimeFn.url = url; $Common.request.ajaxTimeFn.data = data; function action(callback2) { /// <summary> /// 执行 /// </summary> $.ajax({ url: url, data: data, type: ‘POST‘, async: !async ? false : true, //是否同步 false:同步 true:异步 contentType: "application/x-www-form-urlencoded", dataType: "json", success: function (result, textStatus) { if ($.isFunction(callback)) { var json; try { json = JSON.parse(result); } catch (ex) { json = result; } callback(json); } //防止多次提交 if ($.isFunction(callback2)) callback2(); }, error: function (msg) { var n = ""; //防止多次提交 if ($.isFunction(callback2)) callback2(); } }); } } }
}

 

防止ajax重复提交

标签:timeout   延迟   urlencode   else   ram   $.ajax   data   post   com   

原文地址:http://www.cnblogs.com/OleRookie/p/7516656.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!