Sorry, I didn't know how to put the question better. Basically, what I want is, if 2 similar ajax calls are fired before the 1st is finished, instead of sending the second one, just wait for the first one to complete and use that result.
As an example, you can think of an app, which requests Facebook news feed.
User goes from home screen to news feed page
ajax fires to get news feed content from Facebook
user clicks back button, which takes him back to home screen before ajax call is finished
user clicks on news feed again
ajax fires again to get news feed
meanwhile the first request comes back, but is no longer used, because the app is now listening for the second request, which is almost identical
I am saving the news feed content so that I can show it next time user comes to that page, while sending ajax call to update the content. But in the above case, the second time user comes to the page, the values are not saved yet, hence nothing to show before second request comes back.
Assume that it's a one page app, so we don't lose state.
I know it requires good amount of thinking. Any ideas? at least in theory?
Set a global variable = 0 when the page loads. When a request is triggered - if the variable != 1 - fire off the request. Once you have retrieved the requested and output the results; set it back to 0.
Assuming we don't lose state store the results of the ajax call somewhere and when the news feed is loaded again check if the results already exist before making the request again.
Related
I'm making a website with the Steam API.
I was trying it out by getting the friends list of the signed in person.
But the more friends you have, the longer it takes to load the page.
So I made the page start to load the friends as soon as the page is done loading.
If I try to refresh the page or sign out while the page is requesting the friends list, it just keeps on loading until the friend list has been fetched and only then, it refreshes the page or signs out.
How do I fix so I can refresh the page without having to wait for the request to be fuly performed.
Here is the jQuery I use to load in the PHP file:
$(function() {
$('#friends').load("friendstest.php");
});
Please tell me if you need more information.
The website is here.
This can be due to a sessiĆ³n write lock.
If you make the request and it takes a long time, the user session file is locked for writing, so another request can't open it for writing before the previous request release the file, and it is done at the end of the php script.
If your php script is not going to write any data in the session, you can call the session_write_close() function to release the lock (http://php.net/manual/en/function.session-write-close.php) so you enable other request to open the session file.
So at the begining of your friendstest.php and after the session_start, you can call the session_write_close().
Instead of loading the entire friends list in a single call, you can split it into multiple calls with something like:
upto = 0;
function loadMoreFriends() {
$('#friends').append("friendstest.php?limit=10&start="+upto);
upto += 10;
}
In this example, upto records how many friends have been loaded. The php would return only 10 friends and start from where the page is upto.
You would call this function on page load and then whenever you would like to load more friends to the page (scroll to bottom or similar).
I've a login form of a website running IIS/Asp.net which during booking times because extremely sluggish.
At the LOGIN PAGE I'm doing:
$("#submit1").trigger('click') //this will login into PAGE1 using form.submit()
setTimeout(function(){
window.location = "/PAGE2";
},500);
PAGE1 has a GET link to PAGE2.
All I want to know whether it will take me to PAGE2 or PAGE1 ?
Because the IIS server will first process the form submission and send the PAGE1 content. Then it will send the GET request PAGE2 content.
How will the browser(Chrome & Firefox) behave? If it first received the POST response then will it discard the PAGE2 GET response?
Or will it overwrite the POST response(PAGE1) with the GET response(PAGE2)
Assuming POST response(PAGE1) is received and now site is showing PAGE1. After 1 minute the GET response(PAGE2) is received. Now what will the browser do?
I want to depict the sequence(which is example case):
in other words what i understand your question is "which page will my browser load?"
The broswer will Load whatever the most recent request it makes.
if your button click request longer than 500 miliseconds to load a new page resource the button click request should get overwritten to load page 2. If the page loads before 500 miliseconds then it should load page 1.. its a bit like asking what happens if i type www.google.com in my address bar press enter and then enter www.facebook.com... which resource do you think will load?... The most recent request that you make.
By the way window.location = '...' will do a server request just like your asp button click does.
I recomend that you open up google chrome network tab and investigate/observe what calls you make to the server in what sequence...
The code
$("#submit1").trigger('click') //this will login into PAGE1
setTimeout(function(){
window.location = "/PAGE2";
},500);
This will trigger the click event on the element with id submit1. Assume it is a button for login.
Then
There are 2 possibilities because you didn't mentioned whether this is carried out via Ajax or not.
If you used ajax, then if the login process completed within 500mS, then you will successfully log in to PAGE1 and redirect to PAGE2. Because it is ajax.
If you didn't used ajax, then the code
setTimeout(function(){
window.location = "/PAGE2";
},500);
will be ignored. Because at the first event itself, it will take you to PAGE1and the life cycle of this script from Login Page will end. So, you won't get to PAGE2. If you want to go to PAGE2, then you should add the above mentioned code in PAGE1.
Note
The request GET and POST can't do anything with the life cycle of a script if it is not made via Ajax.
Update
No. Both POST and GET won't trigger at a time.
First the POST will trigger.
If it first received the POST response then will it discard the PAGE2 GET response?
Yes
Because the GET request is made from login page and as a result of the POST message, you are already redirected to Page1. So, the GET response won't even catch by the browser.
I'm in trouble. I'm trying to do something which can appear easy but I don't manage to do it.
Here is the situation :
I have 3 different html pages :
The first one called index.html is my main page with a button to lauch a test in AJAX (I'm gonna talk about it after).
The second one corresponds to a redirection to the third page.
The third one has data.
What I wanna do is :
Click on "test" button on the first page and then start an AJAX request on the second one to reach data from the third one (with the redirection) to print it in the first.
I mean the first page calls the second one in AJAX, but the just to redirect on a specific third page. Depending on the third page which is called, data returned to the first page will change.
Do you think it's possible to find a solution to this problem in Javascript ? I hope I have been clear enough.
Thank you in advance for answer.
What you are describing doesn't really make sense. There are methods of redirection that AJAX follows (an HTTP 301 code, for example). However, what you are describing is not that sort of redirect. You are describing Javascript code that, when run, will redirect the browser elsewhere.
However, you are not loading that Javascript with the browser, you are downloading the page via AJAX. Once it is downloaded, you will have the text that represents that page, but in order to find out where it would redirect to when run, you would either have to run it and somehow capture the redirect value (not recommended), or parse it yourself (also not recommended).
There are other options that could work, depending on what you are trying to accomplish:
The server could return an HTTP redirect code when the second page is requested
The second page could instead be a text or JSON file containing a URL, which the first page could read, then request data from that URL.
If there is logic in the second page that determines where the redirect goes, it could be moved to the first page
You could have the second page employ server-side scripting (PHP, etc) to determine what data should be returned and return it directly to the first page
I'll be as direct and as specific as possible.
I'm trying to create Greasemonkey addon that would create graph of winnings/loses on: dead link
As you can see, site has front page which dinamicly shows results of wins / loses and how much did which user win/loose. What I'm trying to do is catch every new entry so I can draw some grapsh and or statistics for user / users.
When I access div/span that should have data, it turns out to be empty. I know that reason behind this is that all divs with data relevant to me are empty on load and that they get populated later on.
What I don't know is how to access that data. I can see (using firebug console) that there are GET requests executed all the time and that in those get requests is data that I need.
Can someone tell me or at least point me into right direction, how to access that data every time it gets refreshed / inserted?
You can try using the $.ajaxSuccess function to specify a function in your script to be called everytime an ajax request completes in the main page. This'll be fired for every successful ajax request, whether it pertains to the data you're talking about or not, but should allow you to re-scrape that section of the document to grab any and all data in it after every successful request. You may want to wrap your callback function in a setTimeout of some kind to make sure their own callbacks have a chance to fire and inject/modify the content before you scrape it. It should still seem instantaneous to the user if you set a timeout of, say, 1-10ms.
http://api.jquery.com/ajaxSuccess/
I display a list of posts on my page with a like button that the user can toggle on or off.
In order to throttle the traffic with my server, I would like to:
send updates for all the posts where the like status has changed at once in the same request
send updates for the modified posts only
update the like status each N seconds, and on page exit only. Not each time a user toggles a like button.
Is this possible with angularjs?
I've written up an example that can be seen here:
http://plnkr.co/edit/imMGTJ75mKJZT7ispD9E?p=preview
I've spent some time on it with commenting, and providing useful information that gets displayed on the page itself when it runs. Change around the delays if you'd like it to run slower if the messages are moving too quickly.
From your question it looks like you want to save when a user makes any changes to a particular post. You proposed checking every x seconds for changes, but this isn't ideal (though it would be simple to implement with a setInterval). You also mentioned saving the changes on page exit, but it's impossible to guarantee that something happens on page exit (a user loosing power for example).
To avoid the above, I would fire the ajax call when the user clicks the "like" button, but throttle them after the first click & store their changes while the throttle timer is running and push all their changes at once after the timer ends.
Here is what my plunker code does in a nutshell:
User "likes" or "unlikes" a post and it will make an Ajax call to the server with the new information on the post. At this point, any new "likes" / "unlikes" gets thrown into a "queue" of posts that need to get updated.
When the first Ajax call is successful, the throttle timer starts. In the example I've provided it is 5 seconds. Any changes to a post ("likes", "unlikes") will be thrown in that same "queue".
After 5 seconds is up, it will check the "queue". If it's empty, no action is taken. If it has items in it (e.g. posts that have changed), then it will make a second ajax call and update the posts on the server.
My example won't mirror what you're working on exactly, but it's the concept that matters. You could modify the code so it doesn't throttle for such a long time, or have it only throttle after x number of ajax calls in a certain amount of time, etc.