javascript - How do I disable and then re-enable a Hammer Event properly using Hammer.js? -
i'm using hammer.js , jquery.hammer.js in order handle multiple different types of events (mostly tap events).
i made wrapper function used any/all tap event listener declarations. function.
var onclick = function(button, callbackfunction, turnbackonafterstartcallback) { if(turnbackonafterstartcallback != false) { turnbackonafterstartcallback = true; } if(!button) { logresult("error: attempted create hammer click event listener without assigning jquery object listen too..."); return; } if(!callbackfunction) { logresult("error: attempted create hammer click event listener without assigning callback function..."); return; } $(button).hammer().on("tap", function(event) { var target = event.target; // disable button can't spam event.... $(target).hammer().off("tap"); // receive event object, incase need it... // call our callbackfunction... if(callbackfunction) { callbackfunction(target); } // renable button future use if need be. if(turnbackonafterstartcallback) { $(target).hammer().on("tap", callbackfunction); } }); };
when register event using function works expected. first disables event listener can't spam event clicking button 100 times... so...
$(target).hammer().off("tap");
then preforms callback functionality if there exists any...
if(callbackfunction) { callbackfunction(target); }
finally re-enable button future use, unless we've specified not turned on...
// renable button future use if need be. if(turnbackonafterstartcallback) { $(target).hammer().on("tap", callbackfunction); }
this works during first event launch... however, once trigger event again callback function sent event , not event.target reason...
if remove .off , .on calls works expected can spammed...
for live example checkout jsfiddle... prints result console... first output correct, after isn't expected. https://jsfiddle.net/xupd7nl1/12/
never mind, had dumb moment there...
the issue was calling event listener directly , not through wrapper function, onclick...
in other words change...
if(turnbackonafterstartcallback) { $(target).hammer().on("tap", callbackfunction); }
to
if(turnbackonafterstartcallback) { onclick(target, callbackfunction, turnbackonafterstartcallback); }
Comments
Post a Comment