js防止多次提交

 1防止多次提交方法1

/**
 * 函数防止多次提交
**/
function Throttle(method, context, params) {
    if (!method.IsUsed)
    {
        method.IsUsed = true;
        method.apply(context, params);
        method.IsUsed = false;
    }
}

调用代码:

/**
 *对外调用
@method Save
 **/
function Save(gatherFun, callbackFun) {
    var validSuccess = __FormVerify(); //验证是否满足保存的条件

    if (validSuccess)
        Throttle(__SaveCore, null, [gatherFun, callbackFun]);
}

/**
 *内部调用保存,实际的保存代码
@method __SaveCore
 **/
function __SaveCore(gatherFun, callbackFun) {
    var dataPost = {};
    dataPost["templateVersionGuid"] = window.TemplateGUID;
    dataPost["EpisodeID"] = window.EpisodeID;
    dataPost["LOGON.CTLOCID"] = session['LOGON.CTLOCID'];
    dataPost["LOGON.WARDID"] = session['LOGON.WARDID'];
    dataPost["LOGON.GROUPDESC"] = session['LOGON.GROUPDESC'];
    dataPost["LOGON.USERID"] = session['LOGON.USERID'];
    dataPost["TemporarySave"] = saveType //0:暂存,1:保存
        $.ajax({
            type: "POST",
            url: WebIp + "/csp/dhcnurmpgetdata.csp?ClassName=NurMp.DHCTempMultData&MethodName=Save",
            data: {
                datapost: JSON.stringify(dataPost)
            },
            success: function (msg) {}
        });
}

2防止多次提交方法2,使用全局变量

var isSave = false; /*是否正在保存*/

/**
 *内部调用保存,实际的保存代码
@method __SaveCore
 **/
function __SaveCore(gatherFun, callbackFun) {
    if (!isSave) {/*如果正在保存,则忽略*/
        isSave = true;
        var dataPost = {};
        dataPost["templateVersionGuid"] = window.TemplateGUID;
        dataPost["EpisodeID"] = window.EpisodeID;
        dataPost["LOGON.CTLOCID"] = session['LOGON.CTLOCID'];
        dataPost["LOGON.WARDID"] = session['LOGON.WARDID'];
        dataPost["LOGON.GROUPDESC"] = session['LOGON.GROUPDESC'];
        dataPost["LOGON.USERID"] = session['LOGON.USERID'];
        dataPost["TemporarySave"] = saveType //0:暂存,1:保存
            $.ajax({
                type: "POST",
                url: WebIp + "/csp/dhcnurmpgetdata.csp?ClassName=NurMp.DHCTempMultData&MethodName=Save",
                data: {
                    datapost: JSON.stringify(dataPost)
                },
                success: function (msg) {
                    isSave = false/*保存结束后修改标志*/
                }
            });
    }

}
/**
 * 拼接URL参数
 * @method UrlJoinParam
 * @param { string } url
 * @param { string } urlParams   URL参数
 **/
function UrlJoinParam(url, urlParams) {
    if (urlParams == "")
        return url;
	if (url.indexOf("?") > -1) {
		url = url + "&" + urlParams;
	} else {
		url = url + "?" + urlParams
	}
	return url;
}
/**
 * 获取URL中的参数
 * @method GetQueryString
 * @param { string } name 要获取的参数的名字
 **/
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var result = window.location.search.substr(1).match(reg);
	if (result != null) {
		return decodeURIComponent(result[2]);
	} else {
		return null;
	}
}
/**
 * 获取URL中的参数
 * @method GetQueryObject
 * @return { object } 以对象方式返回所有的参数
 **/
function GetQueryObject() {
	var url = location.search;
	var theRequest = new Object();

	if (url.indexOf("?") != -1) {
		var str = url.substr(1);
		strs = str.split("&");
		for (var i = 0; i < strs.length; i++) {
		    var item = strs[i];
		    var key = item.split("=")[0];
		    var value = item.split("=")[1];
		    theRequest[key] = decodeURI(value);
		}
	}
	return theRequest;
}

 

你可能感兴趣的:(JavaScript,学习,js面向对象编程)