Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
JQuery Load New Content Into Div More Efficiently

I have a chat system I'm trying to develop and am playing around with jQuery.

At the moment, I have something like this to constantly update the chat to make sure new messages are loading:

setInterval(refreshMessages, 1000);

function refreshMessages() {
    console.log("Refreshing messages");
    $("#conversation_container").load(location.href+" #conversation_container>*","");
}

This ensures that this happens across all browsers, and not just for the user who submitted a new message if I had nested this function call in an AJAX request upon 'submit message button click'.

Obviously, this is probably inefficient since this is being done every 1 second. Is there a way to perhaps, instead of reloading the entire div, just get the new <div class="messages"> that have been added to the conversation and append those? Or any other ways to make this a bit more efficient? Instead of forcing a user to reload the page themselves or querying the page every second.

<div id="conversation_container">
    <div class="message">
      First Message!
    </div>
</div>
<form>
  <input id="conversationID" type="hidden"></input>
  <input id="messageInput" name="message" placeholder="Type your message here" type="text"></input>
  <input id="sendMessage" type="submit" value="Send"></input>
</form>

(There is some Ruby backend here to store the messages and save them to the array of messages that will be shown on the page).

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!