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 7583423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:40:27+00:00 2026-05-30T18:40:27+00:00

I have a chat box using jQuery and php. setInterval( function(){ if($(‘#session_container’).children().length > 0){

  • 0

I have a chat box using jQuery and php.

setInterval(
    function(){

        if($('#session_container').children().length > 0){
            if(localStorage['group_id']){
                var group_id = localStorage['group_id'];
                var session_id = localStorage['session_id'];
                var gsid = 'GSID' + group_id + session_id;
                $('#chatbox').load('php/chatbox.php', {'group_id' : group_id, 'session_id' : session_id}, 
                    function(){
                        $('#chatbox').find('tr:even').addClass('alt');
                        if($('#chatbox').children().length == 0){
                            localStorage.clear();
                            window.location.replace('chatro_main.php');
                        }
                    }
                );
            }else{

            }
        }
    }, 1000);

As you can see from the code above, the script runs every one second which I think is very aggressive and I think it’s also wasting power(my laptop’s battery seems to be draining faster when I was running this). Is there a better way of doing this. Something like not reloading the content while the user is on another tab on the browser window. Or something like updating only the content if another user sent a message?
If you know of another technology that could do this in a more optimal way, you could also mention it.

  • 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-30T18:40:28+00:00Added an answer on May 30, 2026 at 6:40 pm

    There are several possible answers to this question:

    1. Don’t use setInterval, but repeatedly setTimeout and adjust your timeout. The idea is, that you first try like every 10 seconds, and then increase the time, if there is no action. So the intervals would be: 10, 20, 40, message received, 10, 20, message received, 10, … You have to adjust the factor you will actually increase the timeout to fit your needs.

    2. Implement it using WebSockets (see comment of @Christian Varga)

    3. Use node.js as server side language, which interacts with client side javascript pretty well. You can have node.js broadcast messages to all clients, so you actually have a two-way connection there (I think they use WebSockets and as fallback something like comet.js, but I am not sure, I just saw it done once)

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

Sidebar

Related Questions

I have made a chat box like thing in PHP. The only problem is
I have some sort of a jquery chat at one specific PHP page. Of
I have problem with session in cakephp.I have one file chat.php that is in
I have written a small chat app that uses mysql + php to facilitate
I'm trying to animate something using jQuery. UPDATE I have it working the way
I recently created a custom ping box (chat). I have a auto refresh every
i have been trying to get the source of a page in php using
I want to have a chat box (textarea) where if user press Enter then
I have made a chat program with using Winsock2.h and the lib file ws2_32.lib.
I have been working on an ajax/php chat system where users can obviously chat

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.