I have auto request using jquery ajax, I am using this function for detection new chat message & notification case. Sometimes I am thinking again what is the effect if client auto request without finish,
I am worried my server is down because i think this is like DDOS HTTP Throttling.
This is my code
$(function(){
initChat();
});
/*
* initialize chat system
*/
function initChat() {
setTimeout("notifChat()" ,2000);
}
function notifChat() {
$.ajax({
url: '/url',
type:"GET",
data: {id:$("#id").val()},
success:function (data,msg) {
//to do success
}
});
setTimeout("notifChat()" ,2000);
}
My question is
Is possible to down server or make server hung up ?
If it is not better idea any somethink suggestion ?
Note: This is not production ready code, I have not tested it.
A couple weekness of this code:
It does not handle the two http connection limit
strengths:
it can tell if the server return an error (as in server error 404,403,402....)
var failed_requests = 0;
var max = 15;
$(function(){
initChat();
});
/*
* initialize chat system
*/
function initChat()
{
setTimeout(
function()
{
notifChat();
}, 2000)
}
function notifChat() {
$.ajax({
url: '/url',
type:"GET",
data: {id:$("#id").val()},
success:function (data,msg)
{
//to do success
},
complete: function()
{
// either call the function again, or do whatever else you want.
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
failed_requests = failed_requests + 1;
if(failed_requests < max)
{
setTimeout(
function()
{
notifChat();
}, 2000)
}
else
{
alert('We messed up');
}
}
});
}
Related
How to correctly abort an asynchronous ajax request? Whenever I click the stop button, the ajax is still running. Here is my code below:
My js
linhaenviar.forEach(function(value, index) {
setTimeout(
function() {
var xhr = $.ajax({
url: //url here,
type: 'GET',
async: 'true',
success: function(resultado) {
if (resultado.match("okay")) {
approved(resultado + "");
}
else {
removelinha();
}
$('#loaded').html(total);
}
}
);
$("#toStop").click(function () {
xhr.abort()
});
}, 3000 * index);
}
);
I am performing an Ajax (JQuery) request on a php script which iterates. At each iteration I do an "echo" of the value of the loop counter, encoded in JSon. The goal is to manage the display of a progress bar.
By doing this I hoped to intercept and retrieve each of the counter values in a separate response thanks to the "progress" event.
Actually I only have one answer which contains all the counter values.
My research always leads me to information about downloading files, which is not my situation.
Maybe I should use the "onreadystatechange" event, but I don't see how to fit it into my code: $ Ajax parameter or separate function?
If anyone has an idea to solve my problem.
Here is my JS code
function DiffuseOffre(envoi, tab, paquet, dest) {
//$('#cover-spin').show(0);
$("#xhr-rep").css("display", "block");
var server = '/Admin/Offres/DiffuseOffre.php';
$.ajax({
url: server,
type: 'Post',
dataType: 'json',
data: {
envoi: envoi,
tab: tab,
paquet: paquet,
dest: dest
},
xhr: function() {
var xhr = new window.XMLHttpRequest();
xhr.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var compteur = evt.text;
$("#xhr-rep").html(compteur);
}
}, false);
return xhr;
},
success: function(msg) {
//$('#cover-spin').css("display", "none");
$('#xhr-rep').css("display", "none");
if (msg.hasOwnProperty('erreur')) {
$("#dialog-erreur").html(msg.erreur);
$("#dialog-erreur").dialog("open");
$("#dialog-erreur").dialog({
width: '600px',
title: msg.title
})
} else {
$("#dialog-message").html(msg.message);
$("#dialog-message").dialog("open");
$("#dialog-message").dialog({
width: '600px',
title: msg.title
})
if (paquet == 1) {
$("#envoi_" + dest).remove();
$("#diffuser").remove();
}
if (msg.hasOwnProperty('encours')) {
$("#en_cours").html(msg.encours);
}
if (msg.hasOwnProperty('fieldset')) {
$("#" + msg.fieldset).remove();
}
}
}
})
}
I have a problem with my add to cart ajax requests which do not result in a laravel project on which I am working. The problem is that locally everything works perfectly well:
async function add_cart(row_id,variant) {
$('#cover-spin').show(0);
$.ajax({
type: "get",
url: APP_URL + "/cart/add/" + row_id,
data: {quantity: 1,variants: variant},
success: function (response) {
if (response) {
$('#search-keyword').keyup();
refreshCartPage();
updateCartCount();
setTimeout(function(){ $('#cover-spin').hide(0); }, 1000);
toastr.options.closeButton = true;
toastr.success("Produit ajouter au panier");
} else {
refreshCartPage();
setTimeout(function(){ $('#cover-spin').hide(0); }, 1000);
toastr.options.closeButton = true;
toastr.error("Quelque chose c'est mal passé");
}
},
error: function (response) {
refreshCartPage();
setTimeout(function(){ $('#cover-spin').hide(0); }, 1000);
}
});
}
here is the link of the site in question shoppypagne.
Do you have any idea how to fix this?
Thanks in advance. :)
I am trying to display more than 200 videos of a custom playlist (saved in my database). I want to show the whole list in a row, using ajax, with setInterval to let Youtube API breath a bit.
Here is my code :
for(var i in a.playlist_rows) {
iTime += 100;
setTimeout(function() {
$.ajax({
url:'https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id='+a.playlist_rows[i].videoID+'&key=XXX',
method:'GET',
cache:false,
dataType:'json',
success:function(a) {
callPageSuccess(a);
showIn = "playlist_content_show";
$('button#btn_play').prop('disabled', false);
$('button#btn_play_repeat').prop('disabled', false);
},
error:function() {
showError('danger', 'Erreur XHR détectée. Contactez le staff.');
}
});
}, iTime);
}
Actually, because of setInterval(), this is the last video ID that will be called in each ajax call. How to get ride of this ?
Found out the solution by passing an argument in my anonymous function :
for(var i in a.playlist_rows) {
iTime += 100;
setTimeout(function(videoID) {
$.ajax({
url:'https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&id='+videoID+'&key=AIzaSyAHLt89IGVRiB8aAI4hUFpRFxkxQTHAqZo',
method:'GET',
cache:false,
dataType:'json',
success:function(a) {
callPageSuccess(a);
showIn = "playlist_content_show";
$('button#btn_play').prop('disabled', false);
$('button#btn_play_repeat').prop('disabled', false);
},
error:function() {
showError('danger', 'Erreur XHR détectée. Contactez le staff.');
}
});
}, iTime, a.playlist_rows[i].videoID);
}
I want to handle multiple ajax responses. Each request will take different time to complete.
Some request might be success and some might be failure . How to know which request is
success or failure ?
can anyone help me out to this one?
You can use JQuery $.ajax(..)
http://api.jquery.com/jQuery.ajax/
$.ajax({
url: "test.html",
context: document.body,
success: function(){
$(this).addClass("done");
},
statusCode: {
404: function() {
alert('page not found');
},
200: function() {
alert('OK !');
}
}
});
or generar Error callback error : {}
Here is the solution,
var requestCount = 5,
requestComplete = 0,
onAjaxComplete = function () {
requestComplete++;
if (requestComplete >= requestCount) {
// all ajax requests complete
alert('Complete');
}
};
for (var i=0; i<requestCount; i++) {
Ext.Ajax.request({
// #todo: ajax request config
success: function () {onAjaxComplete();},
failure: function () {onAjaxComplete();}
});
}