javascript - clearTimeout is not working -
settimeout not working follwing code:
$("#clkdblclk").click(function(){ var clicktimer=settimeout(function(){ //some code execute },1000); $(this).data("clicktimer",clicktimer); }); $("#clkdblclk").dblclick(function(){ var clicktimer=$(this).data("clicktimer"); cleartimeout(clicktimer); //some ajaxrequest });
the element registered both click , double click events.to cancel out click event on doubleclick, settimeout function registered.i ineger timer id in double click method, cleartimeout not cancelling out function execute. i'm not getting error. in advance.
there no way distinguish between click , double-click, every double-click also triggers 2 separate click events. each of those click events also starting settimeout
, overwrites .data('clicktimer')
value.
here's proof: http://jsfiddle.net/mattball/az6zy/
one solution debounce click
event. simple implementation:
var data_key = 'clicktimer'; $('#clkdblclk').click(function() { var $this = $(this); if ($this.data(data_key)) return; var clicktimer = settimeout(function() { // other stuff $this.removedata(data_key); }, 1000); $this.data(data_key, clicktimer); }) .dblclick(function() { var $this = $(this); var clicktimer = $this.data(data_key); cleartimeout(clicktimer); $this.removedata(data_key); });
Comments
Post a Comment