I have an ajax call which is used to perform a task and correspondingly update a table with the progress status.Now i have another ajax call to fetch the status from the table to show the task progress status to the user.The second call gets called repeatedly for every 2 seconds until the task is complete.But i could not show the task progress status to the user as the second ajax call keeps on loading till the response comes for the first ajax call.
I found a similar question in stackoverflow in the following link and it was suggested that it might be due to session lock and that using session_write_close() might work.I tried it and found no success.
Prevent jQuery AJAX call from waiting for previous call to complete
Is there a way to get the response for the second ajax call even while the first ajax call is still processing?
You can controller your Ajax functions flow using deferring concept of jQuery like
var defered = jQuery.Deferred();
function your_function(){
//do something!
defered.reslove()
return defered.promise
}
your_function.then(function(){
//this will not run till your_function is done
})
learn more at https://api.jquery.com/jQuery.Deferred/
Related
A few times a day, when our website is busy, we have more than 1000 requests per second on our database.
During these busy times, when a user clicks on an element and makes an AJAX call and then clicks another element and makes another AJAX call, the second call will wait for the response of the first call.
How can I have this AJAX calls run simultaneously? Is this time space between two calls because of the server being busy? If yes, how can we handle simultaneous AJAX calls?
I had similar problem as you do in the past. Then it was open session on server side. Even if ajax call was async, then it have to wait for server because of lock on session file.
Try to close session write when you do not write anything and then, check you ajax again.
Here you have reference to proper method: session_write_close
My repeated Ajax call deletes session flash data
I have a Javascript to make an Ajax call it calls my api every 5 seconds to update my user page.
I am returning a view with flash data which it has to keep for the next request but the Ajax call clears it and the next request is not completed.
Is there a way to complete it?
I can think to re-flash on an api call, but that seems weird. Any other way?
Since your ajax call needs access to the session information, you have to load the session, which will count as your access to the flash data. You will need your ajax call to reflash the data.
There is a reflash method on the session that will take care of this for you. You can either call it in the Controller method that handles your ajax call, or better yet, create a new middleware that reflashes session data, and attach the middleware to any route that needs this functionality.
I'm new to LARAVEL. I want to send an AJAX request to a function of controller that this function will run very big loop (takes about 2 minutes). I want to have a button that when click on it, it send a request with AJAX to the controller that stops the function that is running big loop. any idea?
After second click (to stop function) you can send to database information about it. In one of 10 loops function can check database and if function get message "stop" then will stopped.
I want a clarification about AJAX on something I was thinking about...
AJAX is said to be asynchronous - if you want it to be - meaning that if a javascript is run while a page loads and in that an AJAX call is executed the page will continue to load while the server process the AJAX request... So is it safe to assume that it can be liken to a pseudo-parallelism?
Now, what about two different javascripts making an AJAX call consequently. That is, while the page loads it encounters two js function one after the other each making an AJAX call each. Because they are different js functions/ or two different js files do they have to wait for the functions to return before they move on? I think that is the case...
And finally the real question I have... If I have a js function called and in that function I palce TWO AJAX calls then will they be processed semi-simultaneously... Because I was trying something on a webpage of mine and by accident I placed two AJAX calls one after the other and I think that was the reason I had some unexpected results. Unfortunately I was in a hurry to deliver so I didn't pursue it but I was wondering if it world like that, because it would be very convenient for some cases...
Somethign like this
function finalizeReservation(){
var request = $.ajax({
type: 'POST',
url: './scripts/php/reserve.php',
data: {
},
dataType: "json"
});
request.done(function(data,textStatus,jqXHR) {
});
request.fail(function(jqXHR, textStatus, errorThrown) {
});
var request2 = $.ajax({
type: 'POST',
url: './scripts/php/reserve.php',
data: {
},
dataType: "json"
});
request2.done(function(data,textStatus,jqXHR) {
});
request2.fail(function(jqXHR, textStatus, errorThrown) {
});
}
Simply curious.
I was thinking it because I have a web page that records reservations. Before finalizing a reservation though the guest can see the details and think about it. At that period the slot in the database is set to pending and making unavailable to another guest so they don't clash or enter in race conditions (i.e while thinking about it the other finalizes it and so when he tries to finalize it himself he cant or if was careless in the code also finalizes it). The problem is in the guest behaviour, although I have tried to capture all events of the refresh, backpage, exit tab, close window kind so the slot doesn't stay in a pending state if the guest for some reason doesn't close the form the correct way (cancel) not all are captured in all browsers. So I though about making a time limit in the server and if it exceeds it and the state is still pending the state is reversed to free (I have the form on a minute timer, after the minute goes it automatically closes and frees the slot). The problem is that the hosting server doesn't support chron jobs although I don't know much about them so I am not sure how much help they would be and the mysql doesn't support schedulers.
So i thought about making two ajax call and the one to be a timer that will check the state of the slot after some time passes and act accordingly.
Any thoughts are appreciated.
Sorry for the long post.
You don't have a specific question about Ajax, but you do seem to have a pretty good understanding of it and everything you said is correct. At least with jQuery, it's asynchronous by default and unless you use Deferred there is no guarantee that code that executes after the ajax call won't complete before the ajax call.
Your actual question seems to be about how to handle stale locks for the reservations. A couple suggestions:
Move to another shared host. I pay $15 a year for a shared host that allows cron jobs.
Whenever someone visits the page, check all locked reservations and if they are old enough assume the lock is stale.
Another ajax call won't help. What if they close the page? You could try using onbeforeunload, to alert a dialog that will remove the lock, but that's very intrusive.
the execution of code after the ajax functions will continue, without waiting for the ajax functions to complete, unless the code that follows is in the callback of the ajax function.
I need the ability to create up to N amount timers in JavaScript/Ajax that will count like a stopwatch(but have no buttons).
Another issue is that my data will be loaded via another Ajax request so the page will constantly be updating. I'm not sure how I would be able to keep a timer's correct value when that happens.
Thanks!
Javascript already has this functionality. Checkout the
setInterval
setTimeout
functions. Both will let you say "In x number of milliseconds, I want the following callback function to fire". With setTimeout, the callback fires once. With setInterval, the callback fires repeatedly until you tell it to stop.