Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4171916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:30:03+00:00 2026-05-21T00:30:03+00:00

i have a chat box that will load new messages using ajax on my

  • 0

i have a chat box that will load new messages using ajax on my content page and it will listen for new ones every 20s. It is working. However, right now i have another link that will allow a user to load previous messages. And that button does not seem to work even though i have already done a clearTimeout() on the existing ajax function. any idea why?

My scripts:

   //for loading previous messages

    var timer;       

    $('#getoldmessages').click(function() {

        $("#workroom_boxes_chatpast").html('<img src="{{ MEDIA_URL }}images/ui-anim_basic_16x16.gif"/>&nbsp; Loading...');
        $.ajax({
            url: "/messages/{{ chat.key.id }}",
            cache: false,
            success: function(html){
                $("#chatcontent").html(html);
                $("#workroom_boxes_chatpast").html('Loaded');
            }
        });
                    clearTimeout(timer);

});      



   //for new messages

function updateMsg() {
$.ajax({
url: "/recent/messages/{{ chat.key.id }}",
cache: false,
success: function(html){
$("#chatcontent").html(html);
}
});
//alert('repeating');
t = setTimeout(updateMsg, 20000);
}
updateMsg();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T00:30:03+00:00Added an answer on May 21, 2026 at 12:30 am

    setTimeout(this, 20000); is broken, this refers to a jquery object in that context, not a function. Check your javascript console and you’ll see an error.

    You don’t need to clearTimeout to make a separate ajax call. Moreover, your call might not work because you’re referring to the variable timer which has the timeout id from the first call to setTimeout, but any successive call will return a new id. So you need to add in your updateMsg the variable assignment: timer = setTimeout(updateMsg,20000);.

    If your initial code is being executed in a $() block separate from updateMsg, then timer will only be visible in that block and changing that in updateMsg won’t have an effect, another problem. If so, either make timer global, or attach it to a global object, or put updateMsg in that $() block.

    Lastly, you should be using setInterval anyway, instead of recalling setTimeout every time, that’s what it is made for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF chat service that accepts duplex tcp connections. A single duplex
I`m writing a chat using WinSock2 and WinAPI functions. And I have a little
I want to create some kind of AJAX script or call that continuously will
I have developed a small chat client using WPF. In each chat window, it
I have to implement gtalk like chat application using UDP in java. i have
I have a new web app that is packaged as a WAR as part
I have a simple page for a chat system. Test page can be found
I have a chat feature on my website that I want to replicate the
i have been trying to get the source of a page in php using
Lets say I want to write an AJAX chat box, just something simple, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.