Limiting .prepend in jquery
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Building a notification bar that drops down when the user has a new message. After the ajax success, I .prepend that new_data to the existing data. It's set on a timer to check ajax call every 10 seconds. Ajax call checks for message_status = FALSE. So, if I have one new message, the new_message .prepends to the existing data.

My problem is, if the user does not click the "Load new message" (which updates message_status to TRUE) on the notification bar, it will .prepend that SAME new_message again every 10 seconds. Is there a way to stop this from happening?

Do you have your existing code?
sguha almost 8 years ago
Yes, give existing code.
gabrielnahmias almost 8 years ago
awarded to skram

Crowdsource coding tasks.

1 Solution

Winning solution

Initial thought is to prepend the message to a div with an id unique to the message (such as message_id). That way, before pretending your message, you can check to see if a div with that I'd already exists.

How would you write that initial "check if" statement to see if that ID is already being used?
psa almost 8 years ago
For example, check out the source code for this page where @sguha and @gabrielnhamias commented on your initial question. They have unique div IDs (see With jQuery, you can do $("#div-id-goes-here").length == 0 which returns
skram almost 8 years ago
View Timeline