Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>In layman terms, the following code:</p> <pre><code> $("#poolbutton").click(function() { $('#pool_count').css('visibility','hidden'); sendMsg(id); }); </code></pre> <p>Does not tell jQuery to <em>run this when a click happens</em>, but rather <em>add this to the list of things that should be run when a click is triggered</em>.</p> <p>That is to say, if you attach the event multiple times, for example in the following manner:</p> <pre><code> $("#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); }); </code></pre> <p>The result will be (you guessed it) sendMsg() gets called 3 times each click, because each event attached is treated independently.</p> <p>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.</p> <p>The fix? Either unbind click events before binding them again in pool_control():</p> <pre><code>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'); }); } </code></pre> <p>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:</p> <pre><code>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"); } </code></pre> <p>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().</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload