複数のAJAXリクエストに問題があります。たとえば、AJAXリクエストが作成され、データベースにレコードが入力されたときにユーザーがボタンをクリックすると、そのボタンが表示されます。しかし、ユーザーがボタンを複数回クリックすると、同時に複数のAJAX要求が行われ、同じレコードがデータベースに2回入力されます。私はasync: false
を試してみました。クリック時に複数のAJAXリクエストをブロックする
はJavaScript
var myObject = {
demoButton : function(thisIdentity) {
"use strict";
var myObj = this;
$(document).on('click', thisIdentity, function(e) {
e.preventDefault();
var trigger = $(this);
trigger.addClass('inactive_icon');
var param = trigger.attr("rel");
var item = param.split("_");
var section = trigger.attr("data-section");
$.ajax({
type: "POST",
url: "mod/check_login.php",
dataType: "json",
success: function(data)
{
if(data.job == 1)
{
$.post('mod/button_processing.php',{ id : item[0], job : item[1] }, function(data) {
var new_id = item[0] + '_' + data.job + '_' + item[2];
if (data.job != item[1]) {
if (data.job === 0) {
trigger.attr("rel", new_id);
if(section == 'postOpen')
{
trigger.css("color", "#677077");
}
else
{
trigger.css("color", "white");
}
myObject.updateCount(item[0]);
$.ajax({
type: "POST",
data : { aid : item[2], type : 1, pid : item[0] },
url: "mod/mark_notifications.php"
});
}
else if(data.job === 1)
{
trigger.attr("rel", new_id);
trigger.css("color", "red");
myObject.updateCount(item[0]);
$.post('mod/add_notification.php',{ pid : item[0], paId : item[2], type : 1 }, function(data) {
if(data.nid != false)
{
var action = 'like';
myObj.conn.sendMsg(item[2], data.nid, action);
}
}, 'json');
}
else if(data.job === 2)
{
mscAlert({
title: 'Error',
subtitle: 'Try some time later.',
okText: 'Close',
});
}
}
}, 'json');
trigger.removeClass('inactive_icon');
}
else if(data.job == 0)
{
var param = {'c': 2};
$('.element_to_pop_up').bPopup({
content: 'ajax',
contentContainer: '.content',
loadUrl: 'mod/filler.php',
loadData: param,
modalColor: '#fff',
positionStyle: 'fixed',
});
}
}
});
});
}, //other functions
}
$(function() { myObject.likeButton(".like"); }
HTML
<button class="like" //other attributes>Like</button>
同じ要求 がすでに動作しているか、同時AJAX要求をブロックする方法はありますか?
どのような提案や参考資料も高く評価されます。