How can i reload the page or a div using jquery and then catching the update like twitter?
to clarify you know when your on twitter and you get an update telling you have 2 new tweets, i got the ajax referesh page bit sorted but how do i catch the new 开发者_开发百科update and count them!! if i make sense sorry!!
help will be appreciated
cheers!
kindest regards Solomon saleh
The way twitter do it is basically:
- Have a regular AJAX call to a server-side script that returns the number of new tweets
- When this number is greater than 0, the "N new tweets" bar is shown
- Clicking this bar loads the new tweets via another ajax call
One way of catching how many updates there are is to keep track of the last tweet id shown on the user's page (each tweet has a unique id). So, when the page is loaded, the max(tweet id) is, say, 100. Your ajax call to check for updates will be something like:
/check_updates.php?last_seen=100
The check_updates.php
script then basically checks the DB for latest tweets for the logged in user where id > 100. Similarly, when the user clicks "Show tweets", the url called will be something along the lines of:
/show_updates.php?last_seen=100
This will mean that show_updates.php
only returns tweets that haven't been seen before, keeping response times to a minimum.
The examples above send the id of the last tweet back to the server for comparison, but it could equally be done via the current timestamp. The server-side calculations would essentially be the same (look for tweets since time: X rather than tweet id: y) but it has the benefit of the client side not having to keep track of the last tweet id loaded.
You just have to crate a server side page that return to you the amount of new updates since last reload. And using jQuery (or "regular" JS) just get an AJAX call to this page and use it's response to show to the user how many update he/she got
You need to send server the information about the last tweet number or some kind of serial number for it to check with twitter (for ex: the last updated time of tweet) or to check from its own list of tweets to identify what tweets to respond back to the client with.
This is how twitter and friendfeed.com sites deal with to enhance user experience on their sites.
You could try checking out the PUSH mechanism on HTTP or sites like pusherapp.com to push your data to the users of your site.
精彩评论