I am trying to build a simple forum where the questions wil b开发者_开发百科e refreshed automatically . Although it is done ,I have two problems. 1 . Since I retrieve the last update from database,last entry is dulicated. 2 . The div where I put the AJAX response always refrshes . How can I remove the duplicate entries and make it smooth forum ,just like in facebook comment page ?
If I understand your first problem, you're saying you are doing something like:
- Take the user's input (i.e. the new entry)
- Write this input to your database
- Select all entries, including the one you just added
Because you are also separately adding the new reply to the div client-side or something the last entry always shows up twice (but doesn't get written to the database twice).
If this is the case you could solve the problem by either omitting the last entry from your MySQL query (e.g. SELECT * FROM entries WHERE id != (SELECT MAX(id) FROM entries)
) or by simply refraining from processing the entry on the client's side.
Concerning your second problem:
Instead of fetching all entries and replacing all content inside the div with the new list of entries, try only appending the new entry with .innerHTML += ...
or jQuery's .append()
for example. This way you won't experience any 'flickering' or jumping content with the rest of the replies.
精彩评论