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

  • Home
  • SEARCH
  • 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 8152545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:44:39+00:00 2026-06-06T15:44:39+00:00

var json_string=$.parseJSON(document.getElementById(json_db).innerHTML); $(function () { $(#folder_tree).jstree({ contextmenu: {items: customMenu}, json_data : {data:json_string}, plugins :

  • 0
var json_string=$.parseJSON(document.getElementById("json_db").innerHTML);
$(function () 
{
    $("#folder_tree").jstree({ 
        contextmenu: {items: customMenu},
        "json_data" : {data:json_string},
        "plugins" : [ "themes", "json_data", "ui", "contextmenu" ],

    });
});
function customMenu(node)
{
    var items = { };
}



$('#folder_tree').delegate('a', 'contextmenu', function (e)
{

        idn = $(this).parent().attr("id");
        if(idn) pool_control(idn,e); //sendMsg(idn)

});

Hi, I’m getting something really weird in jsTree.
The above code shows tree creation and delegating the right click event.
sendMsg(idn) sends an AJAX request to another page.
pool_control(idn,e) uses the event to show a form next to the clicked node. After the form is filled, it calls sendMsg.

The script works perfectly when I call sendMsg directly in the delegated event handler(instead of pool_control). However, when I call it through pool_control, this happens:
Each pool_control is called, the no. of Ajax requests sent increases by one. The contents of the request appears to be the same, but I have no idea why the no. of ajax requests increases. This doesn’t happen when I call sendMsg directly.

What’s happening? How do I fix this?

pool_control and sendMsg:

function pool_control(id,ev)
{

    $("#pool_count").css("left",ev.pageX);
    $("#pool_count").css("top",ev.pageY);
    $("#pool_count").css("visibility","visible");
    $("#poolbutton").click(function()
    {
        $('#pool_count').css('visibility','hidden');    
        sendMsg(id);
    });
    $("#poolcancel").click(function()
    {
        $('#pool_count').css('visibility','hidden');
    }); 
}

function sendMsg(a,ev)
{

    $('#pool_count').css('visibility','hidden');    
    var factid = $("#factid").val();
    var floor = $("#floor").val();
    var ceiling = $("#ceiling").val();
    var pool_count = $('#poolsize').val();
    var userid = document.getElementById("userid").innerHTML;
    $.ajax(
    {
        type: "POST",
        url: "generator.php",
        data: {what:a,name:factid,author:userid,floor:floor,ceiling:ceiling,pool_count:pool_count},
        async: false,
        cache: false,
        timeout:50000,
    }); 
}

This is a long-polling webpage. sendMsg sends a user-made change in the tree and doesn’t care about the response.
Another function waitForMsg is the long-polling ajax request. When it sees the change in db due to sendMsg, it calls the jstree() function again to remake the tree.
When I make a change in the tree, sendMsg sends 1 request first time, 2 requests seconds time, 3 requests third time, so on.
I’m pretty sure nothing’s wrong in that function, but here it is, just in case

function waitForMsg()
{
    var lastupdate = document.getElementById("lastupdate").innerHTML;
    var msg;
    $.ajax({
        type: "POST",
        url: "oracle.php",
        data: {lastupdate:lastupdate},
        async: true,
        cache: false,
        timeout:20000,

        success: function(data)
        { 
            msg = data.split("@");
            document.getElementById("lastupdate").innerHTML=msg[0];
            document.getElementById("json_db").innerHTML=msg[1];
            initPage(); //a <body onready> function which creates the tree again                
            setTimeout('waitForMsg()', 200 );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
                            setTimeout('waitForMsg()', 1000);

        }
    });
}
  • 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-06T15:44:40+00:00Added an answer on June 6, 2026 at 3:44 pm

    Because of the way events work in jQuery, every time you attach an event using functions such as .click(), the event is “stacked” to a list, not replaced.

    In layman terms, the following code:

        $("#poolbutton").click(function()
        {
            $('#pool_count').css('visibility','hidden');    
            sendMsg(id);
        });
    

    Does not tell jQuery to run this when a click happens, but rather add this to the list of things that should be run when a click is triggered.

    That is to say, if you attach the event multiple times, for example in the following manner:

        $("#poolbutton").click(function()
        {
            $('#pool_count').css('visibility','hidden');    
            sendMsg(id);
        });
    
        $("#poolbutton").click(function()
        {
            $('#pool_count').css('visibility','hidden');    
            sendMsg(id);
        });
    
        $("#poolbutton").click(function()
        {
            $('#pool_count').css('visibility','hidden');    
            sendMsg(id);
        });
    

    The result will be (you guessed it) sendMsg() gets called 3 times each click, because each event attached is treated independently.

    Since new click events are being attached every time pool_control is called, the result is the number of sendMsg() calls, and thus the ajax requests, increases by one for every pool_control() call.

    The fix? Either unbind click events before binding them again in pool_control():

    function pool_control(id,ev)
    {
    
        $("#pool_count").css("left",ev.pageX);
        $("#pool_count").css("top",ev.pageY);
        $("#pool_count").css("visibility","visible");
        $("#poolbutton").unbind('click').click(function()
        {
            $('#pool_count').css('visibility','hidden');    
            sendMsg(id);
        });
        $("#poolcancel").unbind('click').click(function()
        {
            $('#pool_count').css('visibility','hidden');
        }); 
    }
    

    OR, and I would personally rather recommend this way because among other things it has less overhead, attach the events only once in your initialization code. You can attach the event whenever you want, it’s perfectly fine to attach them before calling delegate on contextmenu outside of pool_control() in your example for instance:

    var json_string=$.parseJSON(document.getElementById("json_db").innerHTML);
    $(function () 
    {
        $("#folder_tree").jstree({ 
            contextmenu: {items: customMenu},
            "json_data" : {data:json_string},
            "plugins" : [ "themes", "json_data", "ui", "contextmenu" ],
    
        });
    });
    function customMenu(node)
    {
        var items = { };
    }
    
    $("#poolbutton").click(function()
    {
        $('#pool_count').css('visibility','hidden');    
        sendMsg(id);
    });
    $("#poolcancel").click(function()
    {
        $('#pool_count').css('visibility','hidden');
    }); 
    
    $('#folder_tree').delegate('a', 'contextmenu', function (e)
    {
    
            idn = $(this).parent().attr("id");
            if(idn) pool_control(idn,e); //sendMsg(idn)
    
    });
    
    function pool_control(id,ev)
    {
        $("#pool_count").css("left",ev.pageX);
        $("#pool_count").css("top",ev.pageY);
        $("#pool_count").css("visibility","visible");
    }
    

    This method ensures the events are only attached once, and as long as you don’t destroy #pool_count you are off to go, the events will be fired whenever a click happens without having to care about it in pool_control().

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

Sidebar

Related Questions

var url = /MyApp/pspace/filter; var data = JSON.stringify(myData); $.post( url, data, function(response, textStatus, jqXHR)
With array notation such as the following: $.each(data.feed.entry, function(i, item) { var id =
I'm storing JSON formatted data into a var adds with the following methods: var
Given the following array of json objects: var items = [ {id:1, parentId:0, name:'item1'},
Json String: (response) {19:{id:19,name:Product,sku:123,price:59.50,cost:25.00},20:{id:20,name:Test,sku:456,price:50.00,cost:40.00}} JavaScript Code: var json = $.parseJSON(response); var items = new
Im using jquery and dojo to search a json string $.get('json.txt', function(data) { //alert(data);
I am using the following method to basically create a JSON string. var saveData
See this code: var jsonString = '{id:714341252076979033,type:FUZZY}'; var jsonParsed = JSON.parse(jsonString); console.log(jsonString, jsonParsed); When
I have following Jsonstring var j = { name: John }; alert(j.length); it alerts
var body = jsonString=123; var mybody = body.replace(jsonString=,); console.log(mybody:+mybody); console.log(myhead:+body.replace(jsonString=,)); This this should give

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.