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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:19:05+00:00 2026-06-11T14:19:05+00:00

I’m having trouble with my ajaxPoll chat system. I’m trying to send the last

  • 0

I’m having trouble with my ajaxPoll chat system. I’m trying to send the last Id of the chat message’s div back to php, so then the query can only show results that are higher than the last id sent.

All the chat messages are in a div called chat_log. A message would have a div around it with the class of the message’s id. It would have the text message then the ID of that message, for example: class="message543". Okay so to make it easier to understand, here’s a sample bit of HTML, with the structure and in the right order.

<div id='chat_log'>
    <div class='message435'></div>
    <div class='message436'></div>
    <div class='message437'></div>
    <div class='message438'></div>
    <div class='message439'></div>
    <div class='message440'></div>
    <div class='message441'></div>
</div>

As you can see, there are 7 messages in the div max. Any messages higher than that are wiped out (all if that works fine, just a little bit of extra information.) Another thing to note, I use the jquery library and a jquery plugin found here, credit to this blog post. (Information about what the plugin does can be found there.)


What I’ve tried:

function ajaxGo() {
    $.ajaxPollSettings.pollingType = "interval";
    $.ajaxPollSettings.interval = 700;
    $.ajaxPollSettings.maxInterval = 1000;
    $.ajaxPollSettings.durationUntilMaxInterval = 2000;

    var httpcount = 1;

    var lastDiv = lastDiv = $('#chat_log').find('div:last-child');
    var lastId = lastDiv[0].className.substring(7);

    $.ajaxPoll({
        url: "place_chat/update.php",
        type: "POST",
        data: {
            where: where,
            'last': lastId
        },
        dataType: "html",
        cache: false,

        successCondition: function (result) {
            var responseString = result;

            if (result != '') {
                $(result).appendTo('#chat_log').show();
                lastDiv = lastDiv = $('#chat_log').find('div:last-child');
                lastId = lastDiv[0].className.substring(7);
                console.log(lastId);
                httpcount = httpcount + 1;

                if (!hasFocus && !titleCurrentlyChanging) {
                    changeTitle();
                }

                soundHandle = document.getElementById('soundHandle');
                soundHandle.src = '../Sounds/Message.wav';
                soundHandle.play();
            }
        }

    });
}

$(document).ready(function () {
    $('#chat_log').append('<div class="message0">Test</div>');

    setTimeout(ajaxGo, 500);
}); 

The code takes off the string ‘message’, then grabs the number. The code:

lastDiv = lastDiv = $('#chat_log').find('div:last-child');
lastId = lastDiv[0].className.substring(7);
console.log(lastId);

…works correctly when pasted in the console and outputs the correct, last div‘s message id.

However it does not when ran non-manually (probably because it’s not in a function or in a loop or what not), the code for some reason, only sends ‘0‘, when there are other messages with id‘s of 1000+. I’ve also tried many other things, like setInterval‘s, Etc. One thing I think may be causing the issue is the variable scope, it’s as if when the success function of the poll is being called, it won’t adjust the lastId variable. I’m not entirely sure if this is the case though. If it is the case, I would appreciate support on how to fix the variable scope in order for the code to work correctly.

On a side note, the php is working successfully, so I need no assistance with that.

Important Edit (Make the issue easier to understand): The code

lastDiv = lastDiv = $('#chat_log').find('div:last-child');
lastId = lastDiv[0].className.substring(7);

…works fine, it’s the surrounding area (the structure) of it is what seems to be the problem. Also where is not relevant to this where just tells php where the script was sent from. I am aware that there is a div appended on first called test, that’s to define the first lastId as 0. The Loop should handle getting the latest lastId, but it doesn’t.

What I have discovered:
lastId is not being edited in the ajaxPoll, it gets the right lastId from the div, but does not edit lastId. Which makes it ALMOST definite that it’s a scope problem

  • 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-06-11T14:19:06+00:00Added an answer on June 11, 2026 at 2:19 pm

    One problem is ajaxPoll. You create the data to be sent once and it is not re-evaluated on each poll.

    Another problem is that lastId is local to the ajaxGo function. Move it outside.

    Here is a jsfiddle that uses jQuery.ajax and setInterval:

    var lastId = 0;
    
    function ajaxGo() {
    
        $.ajax({
            url: "/echo/html/",
            type: "POST",
            data: {
                'last': lastId,
                html: '<div class="message' + (parseInt(lastId) + 1) + '">' + (parseInt(lastId) + 1) + '</div>'
            },
            dataType: "html",
    
            success: function(result) {
    
                if (result) {
                    $(result).appendTo('#chat_log').show();
                    lastDiv = $('#chat_log').find('div:last-child');
                    lastId = lastDiv[0].className.substring(7);
                    console.log(lastId);
    
    /*                if (!hasFocus && !titleCurrentlyChanging) {
                        changeTitle();
                    }
    
                    soundHandle = document.getElementById('soundHandle');
                    soundHandle.src = '../Sounds/Message.wav';
                    soundHandle.play();*/
                }
            }
    
        });
    }
    
    var timer = setInterval(ajaxGo, 1000);​
    

    Some code that was not relevant to your question was commented out or omitted.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am trying to render a haml file in a javascript response like so:

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.