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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:28:52+00:00 2026-06-04T04:28:52+00:00

I have a jQuery AJAX function where I check for new messages and if

  • 0

I have a jQuery AJAX function where I check for new messages and if there are any, I display a notification to the user that they have a new message. My function is getNewMessage(); which for now works perfectly on page refresh because everything happens once. However, when I put it in setInterval("getNewMessage()", 20000), I get an infinite loop of notifications. This is understandable but how do I stop it from notifying the user every time but at the same time keep checking for new messages?

This is my getMessage function, sorry for the big function:

function getNewMessage()
{
    /** Retrieve new messages **/
    $.post('',  { newMessage : true}, function(data)
    {      
        data = $.parseJSON(data);

        /** If new messages exist then display  notification and popup **/
        if(data)
        {

            var newMessageDialog = $('#newMessageDialog');

            /** Create popup dialog options **/
            newMessageDialog.dialog(
            {
                autoOpen: false,
                modal: true,
                minWidth: '100',
                width: '800',
                hide: 'fold',
                show: 'drop',
                title: "New Messages",   
                close: function()
                {
                    hideAllNotifications();
                } 
            });

            /** Initialise variables **/
            var html = '';
            var length = 0;
            var total = 0;

            /** For each message we create the html for the message and insert into dialog **/
            $.each(data, function(name, messages)
            {
                length = messages.length;
                total += length;
                /** For grammatical reasons. If length > 1 then "message" is plural**/
                grammar = (length > 1) ? "messages" : "message";

                /** Reverse the messages so the latest message appears at top and earliest message at bottom **/
                messages.reverse();

                var msgs = '';
                for(var i in messages)
                {
                    msgs += '<p>' + messages[i]['date'] + ': ' + messages[i]['message'] + '</p>';
                }

                html += '<a href="uid[' + messages[i]['uid'] + ']" class="popUpLink">' + name + ': ' + length + ' new ' + grammar + '</a>';
                html += '<div class="popUpDialog" id="viewNewMessageDialog" title="New messages from ' + name + '"><div class="flexi_box"><h3 class="hubHeader">' + name + '</h3>' + msgs + '</div></div>';
                html += '</br>';
            });

            /** Insert Content **/
            $('.flexi_box h3', newMessageDialog).after(html);

            /** Bind dialog to popuplinks **/
            popUpDialogs();

            /** Bind click event
             *  If clicked then we assume message has been read so we delete from messages table **/
            $('a.popUpLink', newMessageDialog).on('click', function()
            {
                /** Get userid from href **/
                var uid = $(this).attr('href');
                var start = uid.indexOf('[');
                var end = uid.indexOf(']');
                uid = uid.slice(start + 1, end);
                removeReadMessages(uid);
            });

            var grammar2 = (total > 1) ? 'messages': 'message'
            var notifier = showNotification('info', '<h3>You have ' + total + ' new ' + grammar2 + '. Click here to view new ' + grammar2 + '</h3>');

            /** Open dialog on click **/
            $('.info').on('click', function()
            {               
                /** Trigger Dialog **/
                $('#newMessageDialog').dialog('open').css('maxHeight', $(window).height()-90);
            });

            return true;
        }
    });//end post   
}
  • 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-04T04:28:53+00:00Added an answer on June 4, 2026 at 4:28 am

    Instead of calling setInterval, you should call setTimeout to run the function once.
    Then, in your AJAX callback, if you didn’t get a message, call setTimeout again.

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

Sidebar

Related Questions

I have an AJAX function to check for new messages and then prepend the
I have a jquery-ajax function that sends data to a php script and the
I have written a simple jQuery.ajax function which loads a user control from the
I am using the jQuery $.ajax() function. I have put this into a parent
I have a jquery json ajax call and inside my success function I have
I have the following simple jquery snippet $(document).ready(function () { $.ajax({ url:myjson.json, dataType: 'json',
i send ajax requests with jquery, and i have a function: $('input').ajaxSuccess(function(e, xhr, settings)
A situation I ran across this week: we have a jQuery Ajax call that
Here is the form to have ajax check out user existence. <!DOCTYPE html> <html>
I have two jQuery datepickers that once changed, will trigger some ajax to grab

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.