I have a web application where there is a "Vote" Button. The "vote" button can be click as many times as the user is able within a 40seconds interval.
For each click an asynchronous ajax request is created which inserts data into the database. This works well for few users. When there are more than 300 users clicking the vote button the application cannot response properly causing a timeout error.
Please let me know of ant suggestions for bypassing the issue.
Note that CPU usage is almost 50%.
Try This Code, paste this in your script and than use ajax_call instead of $.ajax
$(function(){
window.queue = $.jqmq({
delay : -1,
batch : 2,
callback:function(options){
$.each(options,function(i,option){
$.ajax({
url : option.url,
type : option.type,
data : option.data,
dataType : option.dataType,
beforeSend : function(){
option.beforeSend();
},
success : function(result){
option.success(result);
},
error : function(xhr,status, error){
option.error(xhr,status, error);
},
complete : function(){
queue.next(false);
}
});
});
},
complete:function() {
console.log("done");
}
});
});
var ajax_call = function(data){
var _defaults = {
url : "",
data : {},
type : "post",
dataType : "",
beforeSend : _default_beforeSend,
success : _default_success,
error : _default_error
}
var options = $.extend(_defaults,data);
if(options.url != ''){
queue.add(options);
}
else{
alert("Url is not defined in ajax_call");
}
}
var _default_success = function(){
alert("Please define 'success' funciton in ajax_call for remove this alert!");
}
var _default_beforeSend = function(){
alert("Please define 'beforeSend' funciton in ajax_call for remove this alert!");
}
var _default_error = function(){
alert("Please define 'error' funciton in ajax_call for remove this alert!");
}