Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Prevent multiple event binding/triggering</h2> <p>Because of interesting jQM loading architecture, multiple event triggering is a constant problem. For example, take a look at this code snipet:</p> <pre><code>$(document).on('pagebeforeshow','#index' ,function(e,data){ $(document).on('click', '#test-button',function(e) { alert('Button click'); }); }); </code></pre> <p>Working jsFiddle example: <a href="http://jsfiddle.net/Gajotres/CCfL4/" rel="nofollow">http://jsfiddle.net/Gajotres/CCfL4/</a></p> <p>Each time you visit page <strong>#index</strong> click event will is going to be bound to button <strong>#test-button</strong>. There are few ways to prevent this problem:</p> <p><strong>Solution 1:</strong></p> <blockquote> <p>In this case you should use function on instead of bind. It is faster and meant to replace bind and delegate.</p> </blockquote> <p>Remove event before you bind it:</p> <pre><code>$(document).on('pagebeforeshow','#index',function(e,data){ $(document).off('click', '#test-button').on('click', '#test-button',function(e) { alert('Button click'); }); }); </code></pre> <p>Working jsFiddle example: <a href="http://jsfiddle.net/Gajotres/K8YmG/" rel="nofollow">http://jsfiddle.net/Gajotres/K8YmG/</a></p> <p>In case you have different events bound to an object:</p> <pre><code>$('#index').on('pagebeforeshow',function(e,data){ $(document).off('click tap', '#test-button').on('click tap', '#test-button',function(e) { alert('Button click'); }); }); </code></pre> <p><strong>Solution 2:</strong></p> <p>Use a jQuery Filter selector, like this:</p> <pre><code>$('#carousel div:Event(!click)').each(function(){ //If click is not bind to #carousel div do something }); </code></pre> <p>Because event filter is not a part of official jQuery framework it can be found here: <a href="http://www.codenothing.com/archives/2009/event-filter/" rel="nofollow">http://www.codenothing.com/archives/2009/event-filter/</a></p> <p>In a nutshell, if speed is your main concern then <strong>Solution 2</strong> is much better then Solution 1. </p> <p><strong>Solution 3:</strong></p> <p>A new one, probably an easiest of them all.</p> <pre><code>$(document).on('pagebeforeshow', '#index', function(){ $(document).on('click', '#test-button',function(e) { if(e.handled !== true) // This will prevent event triggering more then once { alert('Clicked'); event.handled = true; } }); }); </code></pre> <p>Working jsfiddle example: <a href="http://jsfiddle.net/Gajotres/Yerv9/" rel="nofollow">http://jsfiddle.net/Gajotres/Yerv9/</a></p> <p>Tnx to the [sholsinger][2] for this solution: <a href="http://sholsinger.com/archive/2011/08/prevent-jquery-live-handlers-from-firing-multiple-times/" rel="nofollow">http://sholsinger.com/archive/2011/08/prevent-jquery-live-handlers-from-firing-multiple-times/</a></p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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