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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:30:31+00:00 2026-05-25T22:30:31+00:00

I created a messaging system which opens up in a jquery dialog when the

  • 0

I created a messaging system which opens up in a jquery dialog when the user clicks the link. It check ever 5 seconds for new messages. the thing is, after the user closes the dialog it continues to check for messages which is using up a lot of resources if the user does not refresh the page. so is there a way to kill functions after closing the dialog?

Code which open the dialog.

$('body a#link').each(function() {
    var $dialog = $('<div></div>');
    var $link = $(this).one('click', function() {
        $dialog
            .load($link.attr('href'))
            .dialog({
                title: $link.attr('title'),
                width: 500,
                height: 300
            });

        $link.click(function() {
            $dialog.dialog('open');

            return false;
        });

        return false;
    });
});

Messenger.php (which is loaded into the dialog)

<script type="text/javascript">
$(function() {
$("form").submit(function() { return false; });
});

$('#message').keypress(function(e) {
if(e.which == 13) {
    $.ajax({
    type: "POST",
    url: "includes/sendmessage.php",
    data: $("#messageform").serialize(),
    success: function(){
        $("#message").val("");
}
    });
}
});

setInterval(function() {
 var msngr = $.ajax({
  type: "GET",
  url: "includes/checkmessages.php",
  success: function(msg){
        $('div#messages').html(msg);
        $("div#messages").scrollTop($("div#messages")[0].scrollHeight);
    }
});
}, 5000);
</script>
<div id="messages" style="width: 450px; height: 350px; overflow: auto;"><?php
include("includes/dbcon.php");
$result = mysql_query("SELECT * FROM messages WHERE message!='' ORDER BY id LIMIT 50") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
    $username = $row['username'];
    $message = $row['message'];
    echo stripslashes($username) . ": " . stripslashes($message) . "<br />"; }
mysql_close($con); ?></div>
<form id="messageform" method="post" action="">
<input type="text" id="message" width="450px" maxlength="500" name="message" />
</form>
  • 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-25T22:30:32+00:00Added an answer on May 25, 2026 at 10:30 pm

    you’ll want to assign your repeating code a variable

    var refreshTimer=null;
    
    refreshTimer = setInterval(function() {
        var msngr = $.ajax({
            type: "GET",
            url: "includes/checkmessages.php",
            success: function(msg){
                $('div#messages').html(msg);
                $("div#messages").scrollTop($("div#messages")[0].scrollHeight);
            }
        });
    }, 5000);
    

    Then, when the dialog is closed you’ll want to use clearInterval() to stop the timer from re-executing.

    if (refreshTimer) clearInterval(refreshTimer);
    

    UPDATE

    If you want to re-enable the timer, I would throw the whole thing in a function. This way you can call stopMessageRefreshTimer(); or startMessageRefreshTimer(); whenever you want.

    var refreshTimer=null;
    
    function stopMessageRefreshTimer(){
        if (refreshTimer) clearInterval(refreshTimer);
    }
    function startMessageRefreshTimer(){
        refreshTimer = setInterval(function() {
            var msngr = $.ajax({
                type: "GET",
                url: "includes/checkmessages.php",
                success: function(msg){
                    $('div#messages').html(msg);
                    $("div#messages").scrollTop($("div#messages")[0].scrollHeight);
                }
            });
        }, 5000);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a messaging system which allows users to send messages to eachother.
I'm writing a messaging system like Facebook's new Messages system, where the entire back
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I'm currently working on creating a private messaging system, (PHP/MySQL) in which users can
Am creating a messaging system model after facebook's messages. The message inbox is threaded,
On my user inbox from my user messaging system I am building a feature
I've been setting up a user to user messaging system using this rails tutorial
I am trying to create a messaging system between users and organizations - that
I am trying to create a conversations based messaging system. I want to group
I recently created a ajax based instant messaging application and after running for a

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.