ajax同步防止重複提交的兩種方法

1.設置變量法:

var _thisCondition = true;
$("button").click(function() {
	if (!_thisCondition) return false;
	_thisCondition = false;
	setTimeout(function (){_thisCondition = true; }, 60000);

	var url = "test2.php";
	
	$.ajax({
		url: url,
		type: 'POST',
		//async: false,
		success: function (msg) {
			console.log(msg);	
		},
		error: function () {
			console.log("網絡連接失敗");
		}
	});
})

2.設置button的屬性爲disable:

$("button").click(function () {
	$(this).attr('disabled',true);
	$.ajax({
		url : 'test2.php',
		type : 'POST',
		async : false,
		success : function (msg) {
			console.log(msg);
			$(this).attr('disabled',false);
		},
		error : function () {
			console.log("網絡連接失敗");
			$(this).attr('disabled',false);
		}
	});
})

以上兩種方法同步異步均可適用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章